2017-02-16 195 views
0

我使用Unity追蹤一些圖像目標(ARCamera - Vuforia),一切工作正常。我已經在圖像目標等插入了一些2D精靈...但現在我不想顯示所有的目標,只要圖像目標被發現,所以我去DefaultTrackableEventHandler.cs並實例化我的自定義類這樣的:協程無法正常工作 - Unity [C#]

#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 
    } 

現在在我的示例類:

公共類示例: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; 
    } 
} 

} 對不起,代碼量很大,但基本上這只是淡入淡出一些精靈。我想在我的OnTrackingFound在Example類中被調用並且在idk .5秒後在我的一些小精靈中淡出時立即放棄。

謝謝!

編輯 - 解決

所以,腳本附加到遊戲對象必須從Monobehaviour繼承,但比它不能被實例化因此我所做的是: 我需要從DefaultTrackerHandler的OnTrackingFound().. 。所以我編寫了這個類的所有東西[我知道它遠離最好的解決方案,但...],而不是實例化另一個腳本,因爲我說它不可能附加到遊戲對象,因爲它不能繼承來自Monobehaviour。對於公共遊戲對象,我直接從這個也從Monobehvr繼承的類中附加了一切。如果您實例化一個繼承monobehv的腳本,它將爲空。感謝大家!

+0

只是評論 - 的OnTrackableFound()正在對常打電話給我示例類,但其中的StartCoroutine永遠不會被調用。 –

回答

1

與您的代碼問題是,您嘗試實例化Example組件MonoBehaviour調用構造函數與new關鍵字。

千萬不要這樣做。

在Unity3D引擎的所有組件需要被添加到現有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上的事件。希望有所幫助。

+0

我可以從非單行爲腳本中做到這一點嗎?如DefaultTracker ...謝謝 –

0

我已經回答了question這需要在gamedev中這樣的延遲。

基本上你會增加一個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; 
    } 

} 

希望這有助於!乾杯!