Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Hide Animations to improve performence

Discussion in 'Scripting' started by christopher_baumeister, Jun 7, 2018.

  1. christopher_baumeister

    christopher_baumeister

    Joined:
    Apr 29, 2018
    Posts:
    12
    Hey,
    I'm currently working on a project using Vurforia. On the Image Target I'v got several Animation, wich are played on touch Input one after the other. So on the first Input the first Animation starts playing. With the second touch input, the second animations starts and the first one stops.... and so on.

    The Problem is that, every Animation is loaded in the scene waiting for it's start. How can I deactivate the models, when they are not needed?
    Does hiding Objects in the scene improve performence?

    I allready tried to deactivate the game object with the "setActive"-method, but then my touch control is't working anymore.

    This is what I got so far without disable the objects. I'm relatively new to coding. So it might appear not as elegant as you expect. ;)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class animationcontroll : MonoBehaviour {
    7.  
    8.     private GameObject myCurrentObject;
    9.     private Animator myCurrentAnimator;
    10.     private AudioSource myCurrentAudio;
    11.  
    12.     private GameObject myLastObject;
    13.     private Animator myLastAnimator;
    14.     private AudioSource myLastAudio;
    15.  
    16.     List<string> myScenes = new List<string> { "infostand", "title", "slideshow" } // infostand has no animation, just a placeholder
    17.     private int sceneCount = 3;
    18.  
    19.     private int counter;
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.         counter = 0;
    24.         Debug.Log("Counter: " + counter);
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update () {
    29.         if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
    30.         {
    31.             if (counter < sceneCount - 1)
    32.             {
    33.                 counter += 1;
    34.                 Debug.Log("Counter: " + counter);
    35.  
    36.                 myCurrentObject = GameObject.Find(myScenes[counter]);
    37.                 myCurrentAnimator = myCurrentObject.GetComponent<Animator>();
    38.                 myCurrentAudio = myCurrentObject.GetComponent<AudioSource>();
    39.  
    40.                 myCurrentAudio.Play();
    41.                 myCurrentAnimator.Play("play");
    42.                 if (counter > 1)
    43.                 {
    44.                     myLastObject = GameObject.Find(myScenes[counter - 1]);
    45.                     myLastAnimator = myLastObject.GetComponent<Animator>();
    46.                     myLastAudio = myLastObject.GetComponent<AudioSource>();
    47.  
    48.                     myLastAnimator.Play("none");
    49.                     myLastAudio.Stop();
    50.  
    51.                  
    52.                
    53.                 }
    54.             } else
    55.             {
    56.                 myCurrentAudio.Stop();
    57.                 myCurrentAnimator.Play("none");
    58.                 counter = 0;
    59.             }
    60.         }
    61.     }
    62.  
    63. }
    64.  
    65.  
    66.  
    Can anyone help me to make this script just show the current gameobject in the scene?
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Animators have build in culling I think.

    Have much lag do you have?
    What's the profiler is saying?
     
  3. christopher_baumeister

    christopher_baumeister

    Joined:
    Apr 29, 2018
    Posts:
    12
    Right now it feels like the camera is a bit after the the real time movement of the phone. The animation is not lagging, but I'm afraid it does when I attach another model with animation. My plan is to add another model and after that switching to an other scene in VR-Mode.

    Nevertheless I would like to hide the other animation so that the focus is only on the active one.

    I watched the profiler this morning, but it was my first time and i've no idea what it is saying to me.
     
  4. christopher_baumeister

    christopher_baumeister

    Joined:
    Apr 29, 2018
    Posts:
    12
    Can anybody give me advice how to disable the gameobjects?