Search Unity

SendMsgTo - Playable

Discussion in 'Timeline' started by msl_manni, Jul 18, 2017.

  1. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    SendMsgTo - Playable

    This is an example playable to use SendMessage to call any function on a game object from TimeLine.
    This opens up many possibilities for using TimeLine in unique ways.
    There is also a commented out code example for directly manipulating script variables from within TimeLine on the GameObject. Hope this helps. :)

    Usage -
    sendMsgTo - Cube (GameObject)
    sendMsg - ChangeInt (Function Name)
    intValue - 7


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5. using UnityEngine.Timeline;
    6.  
    7. public class SendMsgTo : BasicPlayableBehaviour
    8. {
    9.     public ExposedReference<GameObject> sendMsgTo;
    10.     private GameObject _sendMsgTo;
    11.  
    12.     public string sendMsg;
    13.     //public string sendMsg2;
    14.  
    15.     public int intValue;
    16.  
    17.     public override void OnGraphStart(Playable playable)
    18.     {
    19.         _sendMsgTo = sendMsgTo.Resolve(playable.GetGraph().GetResolver());
    20.     }  
    21.    
    22.     public override void OnBehaviourPlay(Playable playable, FrameData info)
    23.     {
    24.         if (_sendMsgTo != null)
    25.         {
    26.             _sendMsgTo.SendMessage (sendMsg, intValue, SendMessageOptions.DontRequireReceiver);
    27.             //_sendMsgTo.GetComponent<Test> ().diag = sendMsg2;
    28.         }
    29.     }
    30.    
    31.     public override void OnBehaviourPause(Playable playable, FrameData info)
    32.     {
    33.        
    34.     }
    35. }
    36.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour {
    6.  
    7.     public bool test;
    8.     public int secs = 5;
    9.     public float rec;
    10.     public int times;
    11.     public string diag;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.        
    21.     }
    22.  
    23.     public void ChangeInt(int i)
    24.     {
    25.         secs = i;
    26.         times++;
    27.     }
    28. }
     
  2. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
    Thanks!