首页 文章

Coroutine工作不正常 - Unity [C#]

提问于
浏览
0

我已经在图像目标等上插入了一些2D精灵......但是现在我不想在找到图像目标后立即显示所有目标,所以我去了 DefaultTrackableEventHandler.cs 并实例化我的自定义类 Example ,如下所示:

#region PRIVATE_MEMBER_VARIABLES

 private TrackableBehaviour mTrackableBehaviour;

 #endregion // PRIVATE_MEMBER_VARIABLES

 public Example mainClass;

...

private void OnTrackingFound()
    {
    ...
        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");

        mainClass = new Example();
        mainClass.OnTrackableFound(); // Public method on 'Example' Class
    }

现在在我的Example类中:

公共类示例:MonoBehaviour {

public SpriteRenderer forYou;
public SpriteRenderer map;

public Example () {}

private bool isInTransition = false;
private float transition = 1;
private bool isShowing = false;
private float duration = 0;

void Start()
{
    forYou = new SpriteRenderer();
    map = new SpriteRenderer();
}

public void OnTrackableFound() {
    StartCoroutine(FadeCoroutine());
}

private IEnumerator FadeCoroutine() {
    yield return new WaitForSeconds(1);
    Fade(true, .1f);
}

public void OnTrackableLost() {}

public void Fade (bool showing, float duration)
{
    isShowing = showing;
    isInTransition = true;
    this.duration = duration;
    transition = (isShowing) ? 0 : 1;
}

void Update ()
{
    if (Input.GetMouseButtonDown (0)) {
        Fade (true, .1f);
    }
    if (Input.GetMouseButtonUp (0)) {
        Fade (false, .1f);
    }


    if (!isInTransition) {
        return;
    }

    transition += (isShowing) ? Time.deltaTime * (1 / duration) : -Time.deltaTime * (1 / duration);
    forYou.color = Color.Lerp (Color.white, new Color (1, 1, 1, 0), transition);
    map.color = Color.Lerp (Color.white, new Color (1, 1, 1, 0), transition);

    if (transition > 1 || transition < 0) {
        isInTransition = false;
    }
}

很抱歉代码量,但基本上这只是为了淡入/淡出一些精灵 . 我想在Example类中调用我的OnTrackingFound后立即屈服,并且在idk .5秒之后我会在一些精灵中淡出 .

谢谢!

编辑 - 已解决

因此,要将脚本附加到游戏对象,它必须从Monobehaviour继承,但它无法实例化,所以我所做的是:我需要来自DefaultTrackerHandler的OnTrackingFound()...所以我编写了该类的所有内容[我知道它的远离最佳解决方案,但...而不是实例化另一个脚本,因为我说它不可能将它附加到游戏对象,因为它不能从Monobehaviour继承 . 对于公共游戏对象,我直接从这个继承了Monobehvr的类附加了所有内容 . 如果您实例化一个继承monobehv的脚本,它将为null . 感谢大家!

3 回答

  • 0

    您的代码的问题是您尝试使用 new 关键字实例化从 MonoBehaviour 调用构造函数派生的组件 Example .

    永远不要这样做 .

    Unity3D Engine中的所有组件都需要添加到现有的 GameObjectGameObject.AddComponent<T>()

    所以你需要做的是:

    private void OnTrackingFound()
    {
        ...
        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    
        // create new GameObject and add component
        mainClass = new GameObject("MyNewGameobject").AddComponent<Example>();
        mainClass.OnTrackableFound(); // Public method on 'Example' Class
    }
    
  • 0

    看起来每次跟踪新目标时都会调用 mainClass = new Example(); . 这将创建一个新对象 - 尽管可能存在现有对象 . 我不相信你想要这个 .

    而是尝试调用 GameObject gobj = GameObject.Find() (因为GameObject已经存在吗?)函数来获取脚本所附加的现有GameObject . 然后执行 gobj.GetComponent<Example>(). OnTrackableFound() 来在GameObject上调用该事件 . 希望有所帮助 .

  • 1

    我已经回答了question,这在游戏中需要延迟 .

    基本上你会超时增加一个 float 值,当你想要的时间量过去时你会淡化精灵 .

    伪:

    float counter;
    bool isFadeReady = false;
    void Update ()
    {
       if (OnTrackingFound) 
       {
    
        isFadeReady = true;
       }
    
       if (isFadeReady == true) 
       {
        counter +=0.1f;  //change the value for increase speed
       }
    
    
       if(counter>=1.0f) //change the value for desired time
       {
       //Fade your sprite here
       isFadeReady = false;
       }
    
    }
    

    希望这可以帮助!干杯!

相关问题