Beispiel #1
0
        public void SetNameAndLocal()
        {
            GameObject parent = new GameObject();

            SceneNodeView.SetName(parent, "parent");
            SceneNodeView.SetLocalX(parent, 10.0f);
            SceneNodeView.SetLocalY(parent, 20.0f);
            Assert.AreEqual("parent", SceneNodeView.GetName(parent));
            Assert.AreEqual(10.0f, SceneNodeView.GetLocalX(parent));
            Assert.AreEqual(20.0f, SceneNodeView.GetLocalY(parent));
            GameObject child = new GameObject();

            SceneNodeView.SetName(child, "child");
            SceneNodeView.SetLocalX(child, -4.0f);
            SceneNodeView.SetLocalY(child, 3.0f);
            SceneNodeView.AddChild(parent, child);
            Assert.AreEqual(-4.0f, SceneNodeView.GetLocalX(child));
            Assert.AreEqual(6.0f, SceneNodeView.GetWorldX(child));
            Assert.AreEqual(3.0f, SceneNodeView.GetLocalY(child));
            Assert.AreEqual(23.0f, SceneNodeView.GetWorldY(child));
            Vector3 position = new Vector3(5.0f, 1.5f);

            SceneNodeView.SetLocal(child, position);
            Assert.AreEqual(5.0f, SceneNodeView.GetLocalX(child));
            Assert.AreEqual(15.0f, SceneNodeView.GetWorldX(child));
            Assert.AreEqual(1.5f, SceneNodeView.GetLocalY(child));
            Assert.AreEqual(21.5f, SceneNodeView.GetWorldY(child));
            Object.DestroyImmediate(parent);
            Object.DestroyImmediate(child);
        }
Beispiel #2
0
        public void SetWorldScaleY()
        {
            GameObject parent = new GameObject();
            GameObject child  = new GameObject();

            SceneNodeView.AddChild(parent, child);
            SceneNodeView.SetName(parent, "parent");
            SceneNodeView.SetName(child, "child");
            SceneNodeView.SetLocalScaleY(child, 2.0f);
            SceneNodeView.SetLocalScaleY(parent, 3.0f);
            Assert.AreEqual(2.0f,
                            SceneNodeView.GetLocalScaleY(child));
            Assert.AreEqual(6.0f,
                            SceneNodeView.GetWorldScaleY(child));
            SceneNodeView.SetWorldScaleY(child, 12.0f);
            Assert.AreEqual(1.0f,
                            SceneNodeView.GetLocalScaleX(parent));
            Assert.AreEqual(1.0f,
                            SceneNodeView.GetLocalScaleX(child));
            Assert.AreEqual(4.0f,
                            SceneNodeView.GetLocalScaleY(child));
            Assert.AreEqual(12.0f,
                            SceneNodeView.GetWorldScaleY(child));
            Object.DestroyImmediate(parent);
            Object.DestroyImmediate(child);
        }
Beispiel #3
0
        // Try Text first, then TextMesh, then child named "Text".  Else error.
        // Return true if text component was found.
        public static bool SetText(GameObject textOwner, string text, string childName = "Text")
        {
            Text textComponent = textOwner.GetComponent <Text>();

            if (textComponent != null)
            {
                textComponent.text = text;
                return(true);
            }
            TextMesh mesh = textOwner.GetComponent <TextMesh>();

            if (mesh != null)
            {
                mesh.text = text;
                return(true);
            }
            GameObject child = SceneNodeView.GetChild(textOwner, childName);

            if (child != null)
            {
                return(SetText(child, text, childName));
            }
            throw new System.InvalidOperationException(
                      "Expected to set Text or TextMesh component on " + textOwner);
        }
Beispiel #4
0
        // XXX Would be faster with StringBuilder, rather than string concatenation.
        private static void LogInfo(Animator animator, string completedNow, AnimatorStateInfo info,
                                    int layer, string prefix)
        {
            string message = prefix
                             + SceneNodeView.GetPath(animator.gameObject)
                             + ": " + completedNow + " at " + Time.time
                             + " info " + info
                             + " IsName " + info.IsName(completedNow)
                             + " length " + info.length
                             + " normalizedTime " + info.normalizedTime
            ;
            Animation animationStates = animator.GetComponent <Animation>();

            if (null != animationStates)
            {
                foreach (AnimationState state in animationStates)
                {
                    message += "\n    state " + state.name
                               + " time " + state.time
                               + " length " + state.length
                               + " weight " + state.weight
                               + " enabled " + state.enabled
                               + " normalizedTime " + state.normalizedTime
                    ;
                }
            }
            AnimatorClipInfo[] clipInfos = animator.GetCurrentAnimatorClipInfo(layer);
            for (int index = 0, end = clipInfos.Length; index < end; ++index)
            {
                AnimationClip clip = clipInfos[index].clip;
                message += "\n    clip " + clip.name + " length " + clip.length;
            }
            DebugUtil.Log(message);
        }
Beispiel #5
0
        // Return name state that was completed now, or null.
        // Also erases that state, so next time this is called it won't be completed now.
        // Expects time passed since SetState to avoid race when CompletedNow is not called before SetState in the frame.
        // Expects animation is on layer 0.
        // Syncrhonous. Does not depend on event complete animation.
        //
        // http://answers.unity3d.com/questions/351534/how-to-get-current-state-on-mecanim.html
        //
        // Another way might be to get the currently playing animation.
        // http://answers.unity3d.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html
        //
        // Blubberfish says integers will compare faster than strings.
        // http://answers.unity3d.com/questions/407186/how-to-get-current-state-name-on-mecanim.html
        public static string CompletedNow(Animator animator, int layer = 0)
        {
            if (null == animator)
            {
                DebugUtil.Log("AnimationView.CompletedNow: Does animator exist? "
                              + SceneNodeView.GetPath(animator.gameObject));
                return(null);
            }
            if (!animator.isInitialized)
            {
                return(null);
            }
            if (!states.ContainsKey(animator))
            {
                return(null);
            }
            string completedNow = states[animator];

            if (completedNow == null || startTimes[completedNow] == Time.time)
            {
                return(null);
            }
            AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(layer);

            if (info.normalizedTime < 1.0f)
            {
                return(null);
            }
            states[animator] = null;
            if (isVerbose)
            {
                LogInfo(animator, completedNow, info, layer, "AnimationView.CompletedNow: ");
            }
            return(completedNow);
        }
Beispiel #6
0
        // Try Text first, then TextMesh, then child named "Text".  Else error.
        // Return true if text component was found.
        public static string GetText(GameObject textOwner, string childName = "Text")
        {
            TextMeshPro tmp = textOwner.GetComponent <TextMeshPro>();

            if (tmp != null)
            {
                return(tmp.text);
            }
            TextMeshProUGUI tmpUI = textOwner.GetComponent <TextMeshProUGUI>();

            if (tmpUI != null)
            {
                return(tmpUI.text);
            }
            Text textComponent = textOwner.GetComponent <Text>();

            if (textComponent != null)
            {
                return(textComponent.text);
            }
            TextMesh mesh = textOwner.GetComponent <TextMesh>();

            if (mesh != null)
            {
                return(mesh.text);
            }
            GameObject child = SceneNodeView.GetChild(textOwner, childName);

            if (child != null)
            {
                return(GetText(child, childName));
            }
            throw new System.InvalidOperationException(
                      "Expected to set Text or TextMesh component on " + textOwner);
        }
        // Return name state that was completed now, or null.
        // Also erases that state, so next time this is called it won't be completed now.
        // Expects time passed since SetState to avoid race when CompletedNow is not called before SetState in the frame.
        // Expects animation is on layer 0.
        // Syncrhonous. Does not depend on event complete animation.
        //
        // http://answers.unity3d.com/questions/351534/how-to-get-current-state-on-mecanim.html
        //
        // Another way might be to get the currently playing animation.
        // http://answers.unity3d.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html
        //
        // Blubberfish says integers will compare faster than strings.
        // http://answers.unity3d.com/questions/407186/how-to-get-current-state-name-on-mecanim.html
        public static string CompletedNow(GameObject animatorOwner, int layer = 0)
        {
            string   completedNow = null;
            Animator animator     = animatorOwner.GetComponent <Animator>();

            if (null != animator && animator.isInitialized)
            {
                AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(layer);
                if (states.ContainsKey(animatorOwner) && null != states[animatorOwner] &&
                    Time.time != startTimes[states[animatorOwner]] &&
                    1.0f < info.normalizedTime
                    )
                {
                    completedNow = states[animatorOwner];
                    if (isVerbose)
                    {
                        DebugUtil.Log("AnimationView.CompletedNow: "
                                      + SceneNodeView.GetPath(animatorOwner)
                                      + ": " + completedNow + " at " + Time.time
                                      + " info " + info
                                      + " IsName " + info.IsName(states[animatorOwner])
                                      + " length " + info.length
                                      + " normalizedTime " + info.normalizedTime
                                      );
                        Animation animationStates = animator.GetComponent <Animation>();
                        if (null != animationStates)
                        {
                            foreach (AnimationState state in animationStates)
                            {
                                DebugUtil.Log("    state " + state.name
                                              + " time " + state.time
                                              + " length " + state.length
                                              + " weight " + state.weight
                                              + " enabled " + state.enabled
                                              + " normalizedTime " + state.normalizedTime
                                              );
                            }
                        }
                        AnimatorClipInfo[] clips = animator.GetCurrentAnimatorClipInfo(0);
                        foreach (AnimatorClipInfo clip in clips)
                        {
                            DebugUtil.Log("clip " + clip.clip.name + " length " + clip.clip.length);
                        }
                    }
                    states[animatorOwner] = null;
                }
            }
            else
            {
                if (null == animator)
                {
                    DebugUtil.Log("AnimationView.CompletedNow: Does animator exist? "
                                  + SceneNodeView.GetPath(animatorOwner));
                }
            }
            return(completedNow);
        }
Beispiel #8
0
        // namePattern:  Substitute {0} with an index between 0 and to countMax - 1.
        public static List <GameObject> GetChildrenByPattern(GameObject parent, string namePattern, int countMax)
        {
            List <GameObject> children = new List <GameObject>();

            for (int i = 0; i < countMax; i++)
            {
                string     name  = namePattern.Replace("{0}", i.ToString());
                GameObject child = SceneNodeView.GetChild(parent, name);
                children.Add(child);
            }
            return(children);
        }
Beispiel #9
0
        public void SetTextAndGetTextOnTextMesh()
        {
            GameObject node = new GameObject();

            SceneNodeView.SetName(node, "TextMeshOwner");
            node.AddComponent <TextMesh>();
            TextView.SetText(node, "Hello world");
            Assert.AreEqual("Hello world",
                            TextView.GetText(node));
            SceneNodeView.SetVisible(node, false);
            Object.DestroyImmediate(node);
        }
Beispiel #10
0
        public void GetParent()
        {
            GameObject parent = new GameObject();
            GameObject child  = new GameObject();

            SceneNodeView.SetName(parent, "parent");
            SceneNodeView.SetName(child, "child");
            SceneNodeView.AddChild(parent, child);
            Assert.AreEqual("parent", SceneNodeView.GetName(
                                SceneNodeView.GetParent(child)));
            Assert.AreEqual(parent,
                            SceneNodeView.GetParent(child));
            Object.DestroyImmediate(parent);
            Object.DestroyImmediate(child);
        }
Beispiel #11
0
 public void Setup()
 {
     for (int index = 0; index < DataUtil.Length(menus); index++)
     {
         if (DataUtil.Length(buttons) <= index)
         {
             GameObject menu = menus[index];
             buttons.Add(SceneNodeView.GetChildren(menu, true));
         }
     }
     if (null == animatorOwner)
     {
         animatorOwner = gameObject;
     }
 }
        // Call animator.Play instead of animator.SetTrigger, in case the animator is in transition.
        // Test case:  2015-11-15 Enter "SAT".  Type "RAT".  Expect R selected.  Got "R" resets to unselected.
        // http://answers.unity3d.com/questions/801875/mecanim-trigger-getting-stuck-in-true-state.html
        //
        // Do not call until initialized.  Test case:  2015-11-15 Got warning "Animator has not been initialized"
        // http://answers.unity3d.com/questions/878896/animator-has-not-been-initialized-1.html
        //
        // In editor, deleted and recreated animator state transition.  Test case:  2015-11-15 Got error "Transition '' in state 'selcted' uses parameter 'none' which is not compatible with condition type"
        // http://answers.unity3d.com/questions/1070010/transition-x-in-state-y-uses-parameter-z-which-is.html
        //
        // Unity expects not to animate the camera or the root itself.  Instead animate the child of the root.  The root might not move.
        // Test case:  2016-02-13 Animate camera position.  Play.  Camera does not move.  Generate root motion curves.  Apply root motion curves.  Still camera does not move.  Assign animator to parent of camera.  Animate child.  Then camera moves.
        //
        // isTrigger:  If true, then set trigger.  Otherwise play animation.
        public static void SetState(Animator animator, string state, bool isRestart = false, bool isTrigger = false)
        {
            GameObject animatorOwner = animator.gameObject;

            if (null == animator)
            {
                DebugUtil.Log("AnimationView.SetState: Does animator exist? "
                              + SceneNodeView.GetPath(animatorOwner)
                              + ": " + state);
                return;
            }
            if (!animator.isInitialized)
            {
                if (isVerbose)
                {
                    DebugUtil.Log("AnimationView.SetState: Animator is not initialized.");
                }
                return;
            }
            if (!animator.enabled)
            {
                animator.enabled = true;
            }
            if (isTrigger)
            {
                animator.SetTrigger(state);
            }
            else if (isRestart)
            {
                animator.Play(state, -1, 0f);
            }
            else
            {
                animator.Play(state);
            }
            bool isChange = !states.ContainsKey(animatorOwner) ||
                            states[animatorOwner] != state;

            if (isVerbose && (isRestart || isChange))
            {
                DebugUtil.Log("AnimationView.SetState: "
                              + SceneNodeView.GetPath(animatorOwner)
                              + ": " + state + " at " + Time.time);
            }
            states[animatorOwner] = state;
            startTimes[state]     = Time.time;
        }
Beispiel #13
0
        public void GetChildren()
        {
            GameObject parent = new GameObject();

            SceneNodeView.SetName(parent, "parent");
            GameObject child = new GameObject();

            SceneNodeView.SetName(child, "child");
            SceneNodeView.AddChild(parent, child);
            List <GameObject> children = SceneNodeView.GetChildren(parent);

            Assert.AreEqual(1, DataUtil.Length(children));
            Assert.AreEqual("child", SceneNodeView.GetName(children[0]));
            Assert.AreEqual(child, children[0]);
            Object.DestroyImmediate(parent);
            Object.DestroyImmediate(child);
        }
Beispiel #14
0
        public void ToSceneNodeList()
        {
            GameObject parent = new GameObject();

            SceneNodeView.SetName(parent, "parent");
            GameObject child = new GameObject();

            SceneNodeView.SetName(child, "child");
            SceneNodeView.SetLocalX(child, 2.0f);
            SceneNodeView.AddChild(parent, child);
            List <GameObject>     children = SceneNodeView.GetChildren(parent);
            List <SceneNodeModel> nodes    = SceneNodeView.ToSceneNodeList(children);

            Assert.AreEqual(1, DataUtil.Length(nodes));
            Assert.AreEqual("child", nodes[0].name);
            Assert.AreEqual(2.0f, nodes[0].x);
            Assert.AreEqual(0.0f, nodes[0].y);
            Object.DestroyImmediate(parent);
            Object.DestroyImmediate(child);
        }