Search Unity

Dynamic Event System

Discussion in 'Scripting' started by yumupdate, Jul 19, 2013.

  1. yumupdate

    yumupdate

    Joined:
    Nov 20, 2012
    Posts:
    30
    I've been working on system that fires off events one after the other in Unity, but I've hit a snag on my next step.

    Let's say I have a List of Operation objects. Each Operation could perform any number of tasks based on another class that derives from Operation. Example:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [System.Serializable]
    6. public abstract class Operation
    7. {
    8.     public MonoScript scriptToUse = null;
    9.  
    10.     public abstract IEnumerator TriggerOperation();
    11. }
    Each Operation in the list should reference a script like this and know the properties for editing either in Inspector or manually/programmatically (up/down casting? haven't figured this one out yet):

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [System.Serializable]
    6. public class ChangeTransform : Operation {
    7.     public Vector3 translate = Vector3.zero;
    8.     public Transform targetObject = null;
    9.     public float speed = 1.0f;
    10.    
    11.     public override IEnumerator TriggerOperation()
    12.     {
    13.         float rate = 1.0f / speed;
    14.         float time = 0;
    15.  
    16.         Vector3 startPos = targetObject.position;
    17.         Vector3 targetPos;
    18.        
    19.         targetPos = targetObject.position + translate;
    20.        
    21.         while (time < 1.0f)
    22.         {
    23.             float frameTime = Time.deltaTime * rate;
    24.             time += frameTime;
    25.             if (time >= 1.0f) time = 1.0f;
    26.  
    27.             // start the operation routine
    28.             targetObject.position = Vector3.Lerp(startPos, targetPos, time);
    29.  
    30.             yield return null;
    31.         }
    32.     }
    33. }
    I'm having trouble determining where to go after having set it up this way. The initial thought was that people could go into the inspector and select the correct script for what operation they want to execute. Since each operation has its own set of variables that are relevant, I figured it would cut back on the clutter in the inspector. I'm just not sure if this is the best way to set it up now that I've arrived at the point where I have to tie it into the inspector with custom property draws and things like that.

    Does anyone have any insight into how they've achieved results for clean in-sequence operation setup and execution?

    Thanks!
     
    Last edited: Jul 19, 2013