Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Create GameObject toggle animations

Discussion in 'Scripting' started by yelby, Jul 24, 2021.

  1. yelby

    yelby

    Joined:
    Jan 11, 2018
    Posts:
    10
    Greetings, so I'm trying to make a script that generates basically a 1 frame (for both) on and off animation for a gameobject. I don't know how to make it work.

    upload_2021-7-23_20-6-5.png These are basically what I'm trying to make, with nothing more than ON being active = true and OFF active = false kind of situation or well floats for 1.0 and 0.0 respectively.

    I'd love any help in making this!

    Update1
    So I tried to animate it, but I don't know how to add it to the actual timeline.
    Code (CSharp):
    1. private void CreateToggleAnimations(GameObject avatar, AnimatorController target, GameObject tObj)
    2.     {
    3.         Debug.Log("Toggle");
    4.         string path = "Assets/Yelby/Programs/Toggle/" + avatar.name;
    5.         if (!AssetDatabase.IsValidFolder(path))
    6.         {
    7.             Debug.LogError(path + " not available");
    8.             return;
    9.         }
    10.  
    11.         AnimationClip clipF = new AnimationClip();
    12.         AnimationClip clipO = new AnimationClip();
    13.         AnimationEvent tEvent = new AnimationEvent();
    14.  
    15.         //Off
    16.         tEvent.functionName = tObj.name + "_OFF";
    17.         tEvent.objectReferenceParameter = tObj;
    18.         tEvent.intParameter = 0;
    19.         tEvent.time = 0.0f;
    20.         tObj.SetActive(false);
    21.         clipF.AddEvent(tEvent);
    22.         AssetDatabase.CreateAsset(clipF, path + "/" + tObj.name + "_OFF"+".anim");
    23.         AssetDatabase.Refresh();
    24.  
    25.         //ON
    26.         tEvent.functionName = tObj.name + "_ON";
    27.         tEvent.objectReferenceParameter = tObj;
    28.         tEvent.intParameter = 1;
    29.         tEvent.time = 0.0f;
    30.         tObj.SetActive(true);
    31.         clipO.AddEvent(tEvent);
    32.         AssetDatabase.CreateAsset(clipO, path + "/" + tObj.name + "_ON" + ".anim");
    33.         AssetDatabase.Refresh();
    34.     }
    Update2
    Found out my entire thought was completely wrong with what I have above. It was super simple because I didn't know everything was done through curves
    Code (CSharp):
    1. private void CreateToggleAnimations(GameObject avatar, AnimatorController target, GameObject tObj)
    2.     {
    3.         string path = "Assets/Yelby/Programs/Toggle/" + avatar.name;
    4.         if (!AssetDatabase.IsValidFolder(path))
    5.         {
    6.             Debug.LogError(path + " not available");
    7.             return;
    8.         }
    9.  
    10.         Keyframe[] keys = new Keyframe[1];
    11.         AnimationCurve curve;
    12.         string objectPath = tObj.transform.GetHierarchyPath(null);
    13.  
    14.         //Off
    15.         AnimationClip clipF = new AnimationClip();
    16.         clipF.legacy = true;
    17.        
    18.         keys[0] = new Keyframe(0.0f, 0.0f);
    19.         curve = new AnimationCurve(keys);
    20.         clipF.SetCurve(objectPath, typeof(GameObject), "Is Active", curve);
    21.         AssetDatabase.CreateAsset(clipF, path + "/" + tObj.name + "_OFF" + ".anim");
    22.  
    23.         AssetDatabase.Refresh();
    24.  
    25.         //ON
    26.         AnimationClip clipO = new AnimationClip();
    27.         clipO.legacy = true;
    28.  
    29.         keys[0] = new Keyframe(0.0f, 1.0f);
    30.         curve = new AnimationCurve(keys);
    31.         clipO.SetCurve(objectPath, typeof(GameObject), "Is Active", curve);
    32.         AssetDatabase.CreateAsset(clipO, path + "/" + tObj.name + "_ON" + ".anim");
    33.  
    34.         AssetDatabase.Refresh();
    35.     }
     
    Last edited: Jul 24, 2021
  2. May I ask what for?

    The usual way of doing things like this is to call animation event instead and turn the gameobject on or off from C# code.
     
  3. yelby

    yelby

    Joined:
    Jan 11, 2018
    Posts:
    10
    What for, so I don't know if you know anything about the game VRChat. You can make custom avatars and something common is turning off and on props. I want to automate that process because currently we have to "Add a parameter to a special parameter menu, add a parameter to an control and then add a layer. As well as make animations for turning those props on and off.
    Currently I'm stuck at the part of making the .anim files.