【发布时间】:2019-07-19 09:21:16
【问题描述】:
我正在为孩子们做一个教育游戏.. 但我在场景的尽头停了下来 我无法编写代码来开始新场景.. 在第一个脚本中 玩游戏时,直到最后一个场景才会停止。
YouWin.pictureInPlace++;
我搜索了很多,没有找到我的问题,所以我咨询了你。做一个按钮去下一个场景更容易,但我更喜欢自动做 我认为这个任务可以通过布尔值来完成,但它需要引用游戏对象......以及 2 个图像上的脚本。 第一个脚本(经理)在 Canvas 上放了四个图像。 第二个(YouWin)我把空的游戏对象.. 感谢您的帮助
第一个脚本(Manger)
using UnityEngine;
using UnityEngine.EventSystems;
public class Manager : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
Vector2 pos1;
public GameObject pos2;
private bool canMove;
public GameObject winner;
void Start()
{
pos1 = transform.position;
canMove = true;
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log(eventData);
}
public void OnDrag(PointerEventData eventData)
{
if (canMove)
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
float distance = Vector3.Distance(transform.position, pos2.transform.position);
if (distance < 50)
{
transform.position = pos2.transform.position;
transform.localScale = pos2.transform.localScale;
canMove = false;
winner.GetComponent<YouWin>().pictureInPlace++;
}
else
{
transform.position = pos1;
}
}
}
第二个脚本(YouWin)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class YouWin : MonoBehaviour
{
public int NumberOfImages;
public int pictureInPlace;
public string sceneName;
void Update()
{
if (pictureInPlace == NumberOfImages)
{
StartCoroutine(LoadScene());
Debug.Log("You Win!");
}
}
IEnumerator LoadScene()
{
yield return new WaitForSeconds(1.5f);
SceneManager.LoadScene(sceneName);
}
}
【问题讨论】:
-
async是一个关键字,因此您不能调用变量async...您实际上应该从AsyncOperation async = ...得到一个编译器错误。如果你从不使用它,你将它存储在变量中是为了什么?只需致电SceneManager.LoadSceneAsync(sceneName);为什么pictureInPlace是静态的而NumberOfImages不是?你得到Debug.Log“你赢了”吗? -
这是我得到我的代码的教程,但我修改了它link 并且代码工作正常没有错误没有错误直到现在但是关于
SceneManager.LoadSceneAsync(sceneName);或AsyncOperation async = ...这不是问题即时只是尝试代码,但我需要在场景中匹配图像后开始下一个场景即使是取消当前代码并在YouWin脚本中创建一个新代码
标签: c# unity3d boolean 2d scene2d