Beispiel #1
0
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            if (property.serializedObject.isEditingMultipleObjects)
            {
                return(0f);
            }

            if (property.serializedObject.targetObject is ISingleton && SingletonManagerInspector.SingletonCount(property.serializedObject.targetObject.GetType()) > 1)
            {
                _message     = "Multiple Singletons of this type exist, you should purge the scene of duplicates!";
                _messageType = MessageType.Error;
                return(EditorGUIUtility.singleLineHeight * 2f);
            }

            var go = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);

            if (object.ReferenceEquals(go, null))
            {
                _message     = "This Singleton appears to not be attached to a GameObject.";
                _messageType = MessageType.Error;
                return(EditorGUIUtility.singleLineHeight * 2f);
            }

            if (go.HasComponent <SingletonManager>())
            {
                _message     = "This Singleton is managed by a SingletonManager.";
                _messageType = MessageType.Info;
                return(EditorGUIUtility.singleLineHeight * 3f);
            }
            else if (go.GetComponentsAlt <ISingleton>().Count() > 1)
            {
                _message     = "A GameObject with multiple Singletons on it should have a SingletonManager attached!";
                _messageType = MessageType.Warning;
                return(EditorGUIUtility.singleLineHeight * 2f);
            }
            else
            {
                _message     = null;
                _messageType = MessageType.None;
                return(EditorGUIUtility.singleLineHeight);
            }
        }
Beispiel #2
0
            public override SPEntity GetFromSource(System.Type tp, object obj)
            {
                if (Application.isPlaying)
                {
                    return(base.GetFromSource(tp, obj));
                }

                var go = GameObjectUtil.GetGameObjectFromSource(obj);

                if (go != null)
                {
                    var e = go.GetComponentInParent <SPEntity>();
                    if (TypeUtil.IsType(e.GetType(), tp))
                    {
                        return(e);
                    }
                }

                return(null);
            }
Beispiel #3
0
            public override TSub GetFromSource <TSub>(object obj)
            {
                if (Application.isPlaying)
                {
                    return(base.GetFromSource <TSub>(obj));
                }

                var go = GameObjectUtil.GetGameObjectFromSource(obj);

                if (go != null)
                {
                    var e = go.GetComponentInParent <SPEntity>();
                    if (e is TSub)
                    {
                        return(e as TSub);
                    }
                }

                return(null);
            }
        public static T GetAsFromSource <T>(object obj) where T : class
        {
            if (obj == null)
            {
                return(null);
            }
            if (obj is T)
            {
                return(obj as T);
            }
            if (obj is IComponent)
            {
                var c = (obj as IComponent).component;
                if (c is T)
                {
                    return(c as T);
                }
            }
            var go = GameObjectUtil.GetGameObjectFromSource(obj);

            if (go is T)
            {
                return(go as T);
            }

            //if (go != null && ComponentUtil.IsAcceptableComponentType(typeof(T))) return go.GetComponentAlt<T>();
            if (go != null)
            {
                var tp = typeof(T);
                if (typeof(SPEntity).IsAssignableFrom(tp))
                {
                    return(SPEntity.GetEntityFromSource(tp, go) as T);
                }
                else if (ComponentUtil.IsAcceptableComponentType(tp))
                {
                    return(go.GetComponent(tp) as T);
                }
            }

            return(null);
        }
Beispiel #5
0
 protected override void ReflectiveInit(System.Type memberType, object start, object end, object option)
 {
     _start = (start is Trans) ? (Trans)start : Trans.Identity;
     if (end is Trans)
     {
         _end = (Trans)end;
     }
     else if (end is UnityEngine.Transform)
     {
         _end = Trans.GetGlobal((UnityEngine.Transform)end);
     }
     else if (GameObjectUtil.IsGameObjectSource(end))
     {
         _end = Trans.GetGlobal(GameObjectUtil.GetGameObjectFromSource(end).transform);
     }
     else
     {
         _end = Trans.Identity;
     }
     _useSlerp = ConvertUtil.ToBool(option);
 }
Beispiel #6
0
        public override void OnInspectorGUI()
        {
            var rect = EditorGUILayout.GetControlRect(false, 35f);

            rect = new Rect(rect.xMin + 10f, rect.yMin + 5f, rect.width - 20f, rect.height - 10f);

            if (GUI.Button(rect, "Add Spawn Modifier"))
            {
                TypeSelectionDropDownWindow.ShowAndCallbackOnSelect(rect, typeof(ISpawnerModifier), (tp) =>
                {
                    if (tp != null && TypeUtil.IsType(tp, typeof(Component)))
                    {
                        var go = GameObjectUtil.GetGameObjectFromSource(this.SerializedObject.targetObject);
                        if (go != null)
                        {
                            go.AddComponent(tp);
                        }
                    }
                }, false, false);
            }
        }
        protected override void OnSPInspectorGUI()
        {
            this.serializedObject.Update();


            this.DrawDefaultInspectorExcept(PROP_STATEMACHINE, PROP_STATE, PROP_WAITON);

            var stateMachineProp = this.serializedObject.FindProperty(PROP_STATEMACHINE);

            SPEditorGUILayout.PropertyField(stateMachineProp);

            var src = GameObjectUtil.GetGameObjectFromSource(stateMachineProp.objectReferenceValue);

            if (src != null)
            {
                var states = ParentComponentStateSupplier <IAIState> .GetComponentsOnTarg(src, false).ToArray();

                var stateProp = this.serializedObject.FindProperty(PROP_STATE);

                int index = System.Array.IndexOf(states, stateProp.objectReferenceValue);
                var names = (from s in states select EditorHelper.TempContent(s.DisplayName)).ToArray();

                EditorGUI.BeginChangeCheck();
                index = EditorGUILayout.Popup(EditorHelper.TempContent("State"), index, names);
                if (EditorGUI.EndChangeCheck())
                {
                    stateProp.objectReferenceValue = (index >= 0) ? states[index] as UnityEngine.Object : null;
                }
            }
            else
            {
                EditorGUILayout.LabelField("State", "*Select a State Machine first*");
            }


            this.DrawPropertyField(PROP_WAITON);


            this.serializedObject.ApplyModifiedProperties();
        }
Beispiel #8
0
        public void AddNodeModifierType <T>() where T : IStateModifier
        {
            if (_modifierTable == null)
            {
                _modifierTable = new Dictionary <System.Type, IStateModifier[]>();
            }

            var path = _path.Path;
            int cnt  = path.Count;

            IStateModifier[] arr = new IStateModifier[cnt];
            for (int i = 0; i < cnt; i++)
            {
                var p = GameObjectUtil.GetGameObjectFromSource(path.ControlPoint(i));
                if (p != null)
                {
                    arr[i] = p.GetComponent <T>() as IStateModifier;
                }
            }

            _modifierTable[typeof(T)] = arr;
        }
Beispiel #9
0
        public void AttemptAutoStart()
        {
            int i = this.CurrentIndexNormalized;

            if (i < 0 || i >= _trigger.Targets.Count)
            {
                return;
            }

            if (_signal == SignalMode.Auto)
            {
                IAutoSequenceSignal signal;
                var targ = GameObjectUtil.GetGameObjectFromSource(_trigger.Targets[i].Target, true);
                if (targ != null && targ.GetComponentInChildren <IAutoSequenceSignal>(out signal))
                {
                    if (signal != null)
                    {
                        _routine = this.StartRadicalCoroutine(this.DoAutoSequence(signal), RadicalCoroutineDisableMode.Pauses);
                    }
                }
            }
        }
Beispiel #10
0
        public static void EnableTarget(object target, EnableMode mode)
        {
            var go = GameObjectUtil.GetGameObjectFromSource(target);

            if (go != null)
            {
                switch (mode)
                {
                case EnableMode.Disable:
                    go.SetActive(false);
                    break;

                case EnableMode.Enable:
                    go.SetActive(true);
                    break;

                case EnableMode.Toggle:
                    go.SetActive(!go.activeSelf);
                    break;
                }
            }
        }
        private void DrawTargetAnimatorProperty()
        {
            var targWrapperProp = this.serializedObject.FindProperty(PROP_TARGETANIMATOR);
            var targProp = targWrapperProp.FindPropertyRelative(TriggerableTargetObjectPropertyDrawer.PROP_TARGET);

            _targetDrawer.ManuallyConfigured = true;
            
            var label = EditorHelper.TempContent(targWrapperProp.displayName);
            var rect = EditorGUILayout.GetControlRect(true, _targetDrawer.GetPropertyHeight(targWrapperProp, label));
            _targetDrawer.OnGUI(rect, targWrapperProp, label);


            var obj = targProp.objectReferenceValue;
            if (obj == null || i_PlayAnimation.IsAcceptibleAnimator(obj))
                return;

            var go = GameObjectUtil.GetGameObjectFromSource(obj);

            ISPAnimationSource src;
            if (go.GetComponent<ISPAnimationSource>(out src))
            {
                targProp.objectReferenceValue = src as UnityEngine.Object;
                return;
            }

            Animation anim;
            if (go.GetComponent<Animation>(out anim))
            {
                targProp.objectReferenceValue = anim;
                return;
            }

            ISPAnimator animator;
            if (go.GetComponent<ISPAnimator>(out animator))
            {
                targProp.objectReferenceValue = animator as UnityEngine.Object;
                return;
            }
        }
Beispiel #12
0
        public bool Contains(T state)
        {
            if (_container == null)
            {
                return(false);
            }
            var go = GameObjectUtil.GetGameObjectFromSource(state);

            if (go != null)
            {
                if (_includeStatesOnContainer && _container == go)
                {
                    return(true);
                }
                if (_container.transform == go.transform.parent)
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// Reduces obj to a Material source type (Material, Renderer, UI.Graphics), and returns the material used by it.
        /// Uses the sharedMaterial by default.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="reduceFromGameObjectSource">If the object is a GameObjectSource, when true attempts to retrieve a Renderer or UI.Graphics from said source.</param>
        /// <returns></returns>
        public static Material GetMaterialFromSource(object obj, bool reduceFromGameObjectSource = false)
        {
            if (obj is Material)
            {
                return(obj as Material);
            }
            else if (obj is Renderer)
            {
                return((obj as Renderer).sharedMaterial);
            }
            else if (obj is UnityEngine.UI.Graphic)
            {
                return((obj as UnityEngine.UI.Graphic).material ?? (obj as UnityEngine.UI.Graphic).defaultMaterial);
            }

            if (reduceFromGameObjectSource)
            {
                var go = GameObjectUtil.GetGameObjectFromSource(obj);
                if (go == null)
                {
                    return(null);
                }

                var rend = go.GetComponent <Renderer>();
                if (rend != null)
                {
                    return(rend.sharedMaterial);
                }

                var graph = go.GetComponent <UnityEngine.UI.Graphic>();
                if (graph != null)
                {
                    return(graph.material ?? graph.defaultMaterial);
                }
            }

            return(null);
        }
Beispiel #14
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var signalSourceProp = property.FindPropertyRelative("_signalSource");

            SPEditorGUI.PropertyField(position, signalSourceProp, label);

            if (signalSourceProp.objectReferenceValue == null)
            {
                var go = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (go != null && (go.HasComponent <Rigidbody>() || go.HasComponent <Collider>()))
                {
                    signalSourceProp.objectReferenceValue = go;
                }
                else
                {
                    var root = go.FindTrueRoot();
                    if (root != null && (root.HasComponent <Rigidbody>() || root.HasComponent <Collider>()))
                    {
                        signalSourceProp.objectReferenceValue = root;
                    }
                }
            }
        }
        public T GetTarget <T>(object triggerArg) where T : class
        {
            var obj = this.ReduceTarget(triggerArg);

            if (obj == null)
            {
                return(null);
            }

            var result = ObjUtil.GetAsFromSource <T>(obj);

            if (ObjUtil.IsNullOrDestroyed(result) && this.ImplicityReducesEntireEntity && ComponentUtil.IsAcceptableComponentType(typeof(T)))
            {
                //if not configured, and the triggerArg didn't reduce properly, lets search the entity of the 'triggerArg'
                var go = GameObjectUtil.FindRoot(GameObjectUtil.GetGameObjectFromSource(obj));
                if (go == null)
                {
                    return(null);
                }
                result = go.FindComponent <T>();
            }
            return(result);
        }
Beispiel #16
0
        private UnityEngine.Object GetTargetFromSource(UnityEngine.Object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            if (ObjUtil.IsType(obj, this.RestrictionType))
            {
                return(obj);
            }
            if (this.AllowProxy && obj is IProxy)
            {
                return(obj);
            }

            var go = GameObjectUtil.GetGameObjectFromSource(obj);
            var o  = ObjUtil.GetAsFromSource(this.RestrictionType, obj) as UnityEngine.Object;

            if (this.SearchChildren && o == null && go != null)
            {
                o = go.GetComponentInChildren(this.RestrictionType);
            }

            if (this.AllowProxy && o == null && go != null)
            {
                if (this.SearchChildren)
                {
                    o = go.GetComponentInChildren <IProxy>() as Component;
                }
                else
                {
                    o = go.GetComponent <IProxy>() as Component;
                }
            }

            return(o);
        }
        /// <summary>
        /// Reduces obj to source type, and returns a copy of the material on it.
        /// Works like Renderer.material, but also for UI.Graphics.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="reduceFromGameObjectSource"></param>
        /// <returns></returns>
        public static Material CopyMaterialFromSource(object obj, bool reduceFromGameObjectSource = false)
        {
            if (obj is Renderer)
            {
                return((obj as Renderer).material);
            }
            else if (obj is UnityEngine.UI.Graphic)
            {
                var graph  = obj as UnityEngine.UI.Graphic;
                var source = RendererMaterialSource.GetMaterialSource(graph);
                return(source.GetUniqueMaterial());
            }

            if (reduceFromGameObjectSource)
            {
                var go = GameObjectUtil.GetGameObjectFromSource(obj);
                if (go == null)
                {
                    return(null);
                }

                var rend = go.GetComponent <Renderer>();
                if (rend != null)
                {
                    return(rend.material);
                }

                var graph = go.GetComponent <UnityEngine.UI.Graphic>();
                if (graph != null)
                {
                    var source = RendererMaterialSource.GetMaterialSource(graph);
                    return(source.GetUniqueMaterial());
                }
            }

            return(null);
        }
Beispiel #18
0
        public static bool HasObserver(System.Type notificationType, object sender, bool bNotifyEntity = false)
        {
            if (notificationType == null)
            {
                throw new System.ArgumentNullException("notificationType");
            }
            if (!TypeUtil.IsType(notificationType, typeof(Notification)))
            {
                throw new TypeArgumentMismatchException(notificationType, typeof(Notification), "notificationType");
            }
            if (object.ReferenceEquals(sender, null))
            {
                throw new ArgumentNullException("sender");
            }

            if (sender is INotificationDispatcher)
            {
                return((sender as INotificationDispatcher).Observers.HasObserver(notificationType, bNotifyEntity));
            }
            else if (GameObjectUtil.IsGameObjectSource(sender))
            {
                if (bNotifyEntity)
                {
                    var dispatcher = GameObjectUtil.GetGameObjectFromSource(sender).AddOrGetComponent <GameObjectNotificationDispatcher>();
                    return(dispatcher.Observers.HasObserver(notificationType, bNotifyEntity));
                }
                else
                {
                    var dispatcher = GameObjectUtil.GetGameObjectFromSource(sender).GetComponent <GameObjectNotificationDispatcher>();
                    return(dispatcher != null && dispatcher.Observers.HasObserver(notificationType, bNotifyEntity));
                }
            }
            else
            {
                throw new System.ArgumentException("Sender is not a NotificationDispatcher.", "sender");
            }
        }
Beispiel #19
0
        public static void UnsafeRemoveObserver(System.Type notificationType, object sender, NotificationHandler handler)
        {
            if (notificationType == null)
            {
                throw new System.ArgumentNullException("notificationType");
            }
            if (!TypeUtil.IsType(notificationType, typeof(Notification)))
            {
                throw new TypeArgumentMismatchException(notificationType, typeof(Notification), "notificationType");
            }
            if (object.ReferenceEquals(sender, null))
            {
                throw new System.ArgumentNullException("sender");
            }
            if (handler == null)
            {
                throw new System.ArgumentNullException("handler");
            }

            if (sender is INotificationDispatcher)
            {
                (sender as INotificationDispatcher).Observers.UnsafeRemoveObserver(notificationType, handler);
            }
            else if (GameObjectUtil.IsGameObjectSource(sender))
            {
                var dispatcher = GameObjectUtil.GetGameObjectFromSource(sender).GetComponent <GameObjectNotificationDispatcher>();
                if (dispatcher != null)
                {
                    dispatcher.Observers.UnsafeRemoveObserver(notificationType, handler);
                }
            }
            else
            {
                throw new System.ArgumentException("Sender is not a NotificationDispatcher.", "sender");
            }
        }
 public void GetAllTriggersOnTarget(object target, List <ITriggerable> outputColl)
 {
     if (Application.isPlaying)
     {
         EventTriggerEvaluator.Default.GetAllTriggersOnTarget(target, outputColl);
     }
     else
     {
         if (target is IProxy)
         {
             target = (target as IProxy).GetTarget();
         }
         var go = GameObjectUtil.GetGameObjectFromSource(target);
         if (go != null)
         {
             go.GetComponents <ITriggerable>(outputColl);
             outputColl.Sort(TriggerableOrderComparer.Default);
         }
         else if (target is ITriggerable)
         {
             outputColl.Add(target as ITriggerable);
         }
     }
 }
Beispiel #21
0
        protected override void OnSPInspectorGUI()
        {
            this.serializedObject.Update();

            var targ = this.target as AIController;

            if (targ == null)
            {
                return;
            }

            this.DrawPropertyField(EditorHelper.PROP_SCRIPT);

            var sourceProp = this.serializedObject.FindProperty(PROP_STATESOURCE);

            SPEditorGUILayout.PropertyField(sourceProp);

            var cache     = SPGUI.DisableIfPlaying();
            var stateProp = this.serializedObject.FindProperty(PROP_DEFAULTSTATE);

            switch (sourceProp.GetEnumValue <AIStateMachineSourceMode>())
            {
            case AIStateMachineSourceMode.SelfSourced:
            {
                var states = ComponentStateSupplier <IAIState> .GetComponentsOnTarg(targ.gameObject).Cast <Component>().ToArray();

                stateProp.objectReferenceValue = SPEditorGUILayout.SelectComponentField(stateProp.displayName, states, stateProp.objectReferenceValue as Component);
            }
            break;

            case AIStateMachineSourceMode.ChildSourced:
            {
                var states = ParentComponentStateSupplier <IAIState> .GetComponentsOnTarg(targ.gameObject, false);

                var names = (from s in states select EditorHelper.TempContent(GameObjectUtil.GetGameObjectFromSource(s).name + " (" + s.GetType().Name + ")")).ToArray();
                int i     = states.IndexOf(stateProp.objectReferenceValue);
                i = EditorGUILayout.Popup(EditorHelper.TempContent(stateProp.displayName), i, names);
                stateProp.objectReferenceValue = (i >= 0) ? states[i] as UnityEngine.Object : null;
            }
            break;

            default:
            {
                var states = ArrayUtil.Empty <Component>();
                stateProp.objectReferenceValue = SPEditorGUILayout.SelectComponentField(stateProp.displayName, states, stateProp.objectReferenceValue as Component);
            }
            break;
            }

            cache.Reset();


            this.DrawDefaultInspectorExcept(EditorHelper.PROP_SCRIPT, PROP_STATESOURCE, PROP_DEFAULTSTATE);

            this.serializedObject.ApplyModifiedProperties();


            if (Application.isPlaying)
            {
                if (targ.States != null && targ.States.Current != null)
                {
                    var c   = targ.States.Current;
                    var msg = string.Format("Currently active state is {0} ({1}).", c.DisplayName, c.GetType().Name);
                    EditorGUILayout.HelpBox(msg, MessageType.Info);
                }
                else
                {
                    EditorGUILayout.HelpBox("Currently active state is null.", MessageType.Info);
                }
            }
        }
Beispiel #22
0
        private void NormalizeActionAndWeightArrays()
        {
            var go = GameObjectUtil.GetGameObjectFromSource(this.serializedObject.targetObject);

            if (go == null)
            {
                return;
            }

            var propActions    = this.serializedObject.FindProperty(PROP_ACTIONS);
            var propWeights    = this.serializedObject.FindProperty(PROP_WEIGHTS);
            var currentActions = go.GetComponentsAlt <IAIAction>();

            IAIAction[] serializedActions = new IAIAction[propActions.arraySize];
            for (int i = 0; i < serializedActions.Length; i++)
            {
                serializedActions[i] = propActions.GetArrayElementAtIndex(i).objectReferenceValue as IAIAction;
            }

            if (currentActions.Length == serializedActions.Length && currentActions.Compare(serializedActions))
            {
                if (propWeights.arraySize != serializedActions.Length)
                {
                    propWeights.arraySize = serializedActions.Length;
                }
                return;
            }

            var serializedWeights = (EditorHelper.GetTargetObjectOfProperty(propWeights) as DiminishingWeightOverDuration[]);

            if (serializedWeights == null)
            {
                serializedWeights = new DiminishingWeightOverDuration[] { }
            }
            ;
            else
            {
                serializedWeights = serializedWeights.ToArray();
            }

            propActions.arraySize = currentActions.Length;
            var   weights       = new List <DiminishingWeightOverDuration>();
            float defaultWeight = this.serializedObject.FindProperty(PROP_DEFAULTWEIGHT).floatValue;

            for (int i = 0; i < currentActions.Length; i++)
            {
                propActions.GetArrayElementAtIndex(i).objectReferenceValue = currentActions[i] as Component;

                int j = serializedActions.IndexOf(currentActions[i]);
                if (j >= 0 && j < serializedWeights.Length && serializedWeights[j] != null)
                {
                    weights.Add(serializedWeights[j]);
                }
                else
                {
                    Debug.Log(currentActions[i].GetType().Name + " had no weight");
                    weights.Add(new DiminishingWeightOverDuration(defaultWeight));
                }
            }
            this.serializedObject.ApplyModifiedProperties();
            EditorHelper.SetTargetObjectOfProperty(propWeights, weights.ToArray());
            this.serializedObject.Update();
        }
        private System.Collections.IEnumerable ReduceTargets(object triggerArg)
        {
            switch (_find)
            {
            case FindCommand.Direct:
            {
                object obj = (_configured) ? _target : triggerArg;
                if (ObjUtil.IsNullOrDestroyed(obj))
                {
                    yield break;
                }
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    yield return(obj);

                    break;

                case ResolveByCommand.WithTag:
                {
                    var go = GameObjectUtil.GetGameObjectFromSource(obj);
                    if (go.HasTag(_queryString))
                    {
                        yield return(obj);
                    }
                }
                break;

                case ResolveByCommand.WithName:
                {
                    var go = GameObjectUtil.GetGameObjectFromSource(obj);
                    if (go.CompareName(_queryString))
                    {
                        yield return(obj);
                    }
                }
                break;

                case ResolveByCommand.WithType:
                {
                    var o = ObjUtil.GetAsFromSource(TypeUtil.FindType(_queryString), GameObjectUtil.GetGameObjectFromSource(obj));
                    if (o != null)
                    {
                        yield return(o);
                    }
                }
                break;
                }
            }
            break;

            case FindCommand.FindParent:
            {
                Transform trans = GameObjectUtil.GetTransformFromSource((_configured) ? _target : triggerArg);
                if (trans == null)
                {
                    yield break;
                }
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                {
                    var t = trans.parent;
                    if (t != null)
                    {
                        yield return(t);
                    }
                }
                break;

                case ResolveByCommand.WithTag:
                {
                    foreach (var p in GameObjectUtil.GetParents(trans))
                    {
                        if (p.HasTag(_queryString))
                        {
                            yield return(p);
                        }
                    }
                }
                break;

                case ResolveByCommand.WithName:
                {
                    foreach (var p in GameObjectUtil.GetParents(trans))
                    {
                        if (p.CompareName(_queryString))
                        {
                            yield return(p);
                        }
                    }
                }
                break;

                case ResolveByCommand.WithType:
                {
                    var tp = TypeUtil.FindType(_queryString);
                    foreach (var p in GameObjectUtil.GetParents(trans))
                    {
                        var o = ObjUtil.GetAsFromSource(tp, p);
                        if (o != null)
                        {
                            yield return(o);
                        }
                    }
                }
                break;
                }
            }
            break;

            case FindCommand.FindInChildren:
            {
                Transform trans = GameObjectUtil.GetTransformFromSource((_configured) ? _target : triggerArg);
                if (trans == null)
                {
                    yield break;
                }
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    if (trans.childCount > 0)
                    {
                        yield return(trans.GetChild(0));
                    }
                    break;

                case ResolveByCommand.WithTag:
                    if (trans.childCount > 0)
                    {
                        using (var lst = TempCollection.GetList <Transform>())
                        {
                            GameObjectUtil.GetAllChildren(trans, lst);
                            for (int i = 0; i < lst.Count; i++)
                            {
                                if (lst[i].HasTag(_queryString))
                                {
                                    yield return(lst[i]);
                                }
                            }
                        }
                    }
                    break;

                case ResolveByCommand.WithName:
                    if (trans.childCount > 0)
                    {
                        using (var lst = TempCollection.GetList <Transform>())
                        {
                            GameObjectUtil.GetAllChildren(trans, lst);
                            for (int i = 0; i < lst.Count; i++)
                            {
                                if (lst[i].CompareName(_queryString))
                                {
                                    yield return(lst[i]);
                                }
                            }
                        }
                    }
                    break;

                case ResolveByCommand.WithType:
                    if (trans.childCount > 0)
                    {
                        var tp = TypeUtil.FindType(_queryString);
                        using (var lst = TempCollection.GetList <Transform>())
                        {
                            GameObjectUtil.GetAllChildren(trans, lst);
                            for (int i = 0; i < lst.Count; i++)
                            {
                                var o = ObjUtil.GetAsFromSource(tp, lst[i]);
                                if (o != null)
                                {
                                    yield return(o);
                                }
                            }
                        }
                    }
                    break;
                }
            }
            break;

            case FindCommand.FindInEntity:
            {
                GameObject entity = GameObjectUtil.GetRootFromSource((_configured) ? _target : triggerArg);
                if (entity == null)
                {
                    yield break;
                }
                ;

                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    yield return(entity);

                    break;

                case ResolveByCommand.WithTag:
                {
                    foreach (var o in entity.FindAllWithMultiTag(_queryString))
                    {
                        yield return(o);
                    }
                }
                break;

                case ResolveByCommand.WithName:
                {
                    foreach (var o in GameObjectUtil.FindAllByName(entity.transform, _queryString))
                    {
                        yield return(o);
                    }
                }
                break;

                case ResolveByCommand.WithType:
                {
                    var tp = TypeUtil.FindType(_queryString);
                    using (var lst = TempCollection.GetList <Transform>())
                    {
                        GameObjectUtil.GetAllChildrenAndSelf(entity.transform, lst);
                        for (int i = 0; i < lst.Count; i++)
                        {
                            var o = ObjUtil.GetAsFromSource(tp, lst[i]);
                            if (o != null)
                            {
                                yield return(o);
                            }
                        }
                    }
                }
                break;
                }
            }
            break;

            case FindCommand.FindInScene:
            {
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                {
                    var go = GameObjectUtil.GetGameObjectFromSource((_configured) ? _target : triggerArg);
                    if (go != null)
                    {
                        yield return(go);
                    }
                }
                break;

                case ResolveByCommand.WithTag:
                {
                    foreach (var o in GameObjectUtil.FindGameObjectsWithMultiTag(_queryString))
                    {
                        yield return(o);
                    }
                }
                break;

                case ResolveByCommand.WithName:
                {
                    foreach (var o in GameObjectUtil.FindAllByName(_queryString))
                    {
                        yield return(o);
                    }
                }
                break;

                case ResolveByCommand.WithType:
                {
                    foreach (var o in ObjUtil.FindAll(SearchBy.Type, _queryString))
                    {
                        yield return(o);
                    }
                }
                break;
                }
            }
            break;

            case FindCommand.FindEntityInScene:
            {
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                {
                    var go = GameObjectUtil.GetGameObjectFromSource((_configured) ? _target : triggerArg);
                    if (go != null)
                    {
                        yield return(go);
                    }
                }
                break;

                case ResolveByCommand.WithTag:
                {
                    var e = SPEntity.Pool.GetEnumerator();
                    while (e.MoveNext())
                    {
                        if (e.Current.HasTag(_queryString))
                        {
                            yield return(e.Current);
                        }
                    }
                }
                break;

                case ResolveByCommand.WithName:
                {
                    var e = SPEntity.Pool.GetEnumerator();
                    while (e.MoveNext())
                    {
                        if (e.Current.CompareName(_queryString))
                        {
                            yield return(e.Current);
                        }
                    }
                }
                break;

                case ResolveByCommand.WithType:
                {
                    var e  = SPEntity.Pool.GetEnumerator();
                    var tp = TypeUtil.FindType(_queryString);
                    while (e.MoveNext())
                    {
                        var o = e.Current.GetComponent(tp);
                        if (o != null)
                        {
                            yield return(o);
                        }
                    }
                }
                break;
                }
            }
            break;
            }
        }
        private object ReduceTarget(object triggerArg)
        {
            var targ = _target;

            if (targ is IProxy)
            {
                targ = (targ as IProxy).GetTarget(triggerArg) as UnityEngine.Object;
            }

            switch (_find)
            {
            case FindCommand.Direct:
            {
                object obj = (_configured) ? targ : triggerArg;
                if (ObjUtil.IsNullOrDestroyed(obj))
                {
                    return(null);
                }
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    return(obj);

                case ResolveByCommand.WithTag:
                    return(GameObjectUtil.GetGameObjectFromSource(obj).HasTag(_queryString) ? obj : null);

                case ResolveByCommand.WithName:
                    return(GameObjectUtil.GetGameObjectFromSource(obj).CompareName(_queryString) ? obj : null);

                case ResolveByCommand.WithType:
                    return(ObjUtil.GetAsFromSource(TypeUtil.FindType(_queryString), GameObjectUtil.GetGameObjectFromSource(obj)) != null ? obj : null);
                }
            }
            break;

            case FindCommand.FindParent:
            {
                Transform trans = GameObjectUtil.GetTransformFromSource((_configured) ? targ : triggerArg);
                if (trans == null)
                {
                    return(null);
                }
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    return(trans.parent);

                case ResolveByCommand.WithTag:
                    return(trans.FindParentWithTag(_queryString));

                case ResolveByCommand.WithName:
                    return(trans.FindParentWithName(_queryString));

                case ResolveByCommand.WithType:
                {
                    var tp = TypeUtil.FindType(_queryString);
                    foreach (var p in GameObjectUtil.GetParents(trans))
                    {
                        var o = ObjUtil.GetAsFromSource(tp, p);
                        if (o != null)
                        {
                            return(o);
                        }
                    }
                    return(null);
                }
                }
            }
            break;

            case FindCommand.FindInChildren:
            {
                Transform trans = GameObjectUtil.GetTransformFromSource((_configured) ? targ : triggerArg);
                if (trans == null)
                {
                    return(null);
                }
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    return((trans.childCount > 0) ? trans.GetChild(0) : null);

                case ResolveByCommand.WithTag:
                    if (trans.childCount > 0)
                    {
                        using (var lst = TempCollection.GetList <Transform>())
                        {
                            GameObjectUtil.GetAllChildren(trans, lst);
                            for (int i = 0; i < lst.Count; i++)
                            {
                                if (lst[i].HasTag(_queryString))
                                {
                                    return(lst[i]);
                                }
                            }
                        }
                    }
                    break;

                case ResolveByCommand.WithName:
                    if (trans.childCount > 0)
                    {
                        return(trans.FindByName(_queryString));
                    }
                    break;

                case ResolveByCommand.WithType:
                    if (trans.childCount > 0)
                    {
                        var tp = TypeUtil.FindType(_queryString);
                        using (var lst = TempCollection.GetList <Transform>())
                        {
                            GameObjectUtil.GetAllChildren(trans, lst);
                            for (int i = 0; i < lst.Count; i++)
                            {
                                var o = ObjUtil.GetAsFromSource(tp, lst[i]);
                                if (o != null)
                                {
                                    return(o);
                                }
                            }
                        }
                    }
                    break;
                }
            }
            break;

            case FindCommand.FindInEntity:
            {
                GameObject entity = GameObjectUtil.GetRootFromSource((_configured) ? targ : triggerArg);
                if (entity == null)
                {
                    return(null);
                }

                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    return(entity);

                case ResolveByCommand.WithTag:
                    return(entity.FindWithMultiTag(_queryString));

                case ResolveByCommand.WithName:
                    return(entity.FindByName(_queryString));

                case ResolveByCommand.WithType:
                {
                    var tp = TypeUtil.FindType(_queryString);
                    foreach (var t in GameObjectUtil.GetAllChildrenAndSelf(entity))
                    {
                        var o = ObjUtil.GetAsFromSource(tp, t);
                        if (o != null)
                        {
                            return(o);
                        }
                    }
                    return(null);
                }
                }
            }
            break;

            case FindCommand.FindInScene:
            {
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    return(GameObjectUtil.GetGameObjectFromSource((_configured) ? targ : triggerArg));

                case ResolveByCommand.WithTag:
                    return(GameObjectUtil.FindWithMultiTag(_queryString));

                case ResolveByCommand.WithName:
                    return(GameObject.Find(_queryString));

                case ResolveByCommand.WithType:
                    return(ObjUtil.Find(SearchBy.Type, _queryString));
                }
            }
            break;

            case FindCommand.FindEntityInScene:
            {
                switch (_resolveBy)
                {
                case ResolveByCommand.Nothing:
                    return(GameObjectUtil.GetGameObjectFromSource((_configured) ? targ : triggerArg));

                case ResolveByCommand.WithTag:
                {
                    var e = SPEntity.Pool.GetEnumerator();
                    while (e.MoveNext())
                    {
                        if (e.Current.HasTag(_queryString))
                        {
                            return(e.Current);
                        }
                    }
                }
                break;

                case ResolveByCommand.WithName:
                {
                    var e = SPEntity.Pool.GetEnumerator();
                    while (e.MoveNext())
                    {
                        if (e.Current.CompareName(_queryString))
                        {
                            return(e.Current);
                        }
                    }
                }
                break;

                case ResolveByCommand.WithType:
                {
                    var e  = SPEntity.Pool.GetEnumerator();
                    var tp = TypeUtil.FindType(_queryString);
                    while (e.MoveNext())
                    {
                        var o = e.Current.GetComponentInChildren(tp);
                        if (o != null)
                        {
                            return(o);
                        }
                    }
                }
                break;
                }
            }
            break;
            }

            return(null);
        }
        private void DrawValueFieldInValueMode(Rect position, SerializedProperty property, VariantReference.EditorHelper helper)
        {
            if (helper.Target == null)
            {
                return;
            }
            var variant = helper.Target;

            if (this.RestrictVariantType && helper._type != this.VariantTypeRestrictedTo)
            {
                helper.PrepareForValueTypeChange(this.VariantTypeRestrictedTo);
                GUI.changed = true; //force change
            }

            var r0 = new Rect(position.xMin, position.yMin, 90.0f, EditorGUIUtility.singleLineHeight);
            var r1 = new Rect(r0.xMax, position.yMin, position.xMax - r0.xMax, EditorGUIUtility.singleLineHeight);

            var cache = SPGUI.DisableIf(this.RestrictVariantType);

            EditorGUI.BeginChangeCheck();
            var valueType = (VariantType)EditorGUI.EnumPopup(r0, GUIContent.none, variant.ValueType);

            if (EditorGUI.EndChangeCheck())
            {
                helper.PrepareForValueTypeChange(valueType);
            }
            cache.Reset();

            if (_typeRestrictedTo.IsEnum)
            {
                variant.IntValue = ConvertUtil.ToInt(EditorGUI.EnumPopup(r1, ConvertUtil.ToEnumOfType(_typeRestrictedTo, variant.IntValue)));
            }
            else
            {
                switch (valueType)
                {
                case VariantType.Null:
                    cache = SPGUI.Disable();
                    EditorGUI.TextField(r1, "Null");
                    cache.Reset();
                    break;

                case VariantType.String:
                    variant.StringValue = EditorGUI.TextField(r1, variant.StringValue);
                    break;

                case VariantType.Boolean:
                    variant.BoolValue = EditorGUI.Toggle(r1, variant.BoolValue);
                    break;

                case VariantType.Integer:
                    variant.IntValue = EditorGUI.IntField(r1, variant.IntValue);
                    break;

                case VariantType.Float:
                    variant.FloatValue = EditorGUI.FloatField(r1, variant.FloatValue);
                    break;

                case VariantType.Double:
                    variant.DoubleValue = ConvertUtil.ToDouble(EditorGUI.TextField(r1, variant.DoubleValue.ToString()));
                    break;

                case VariantType.Vector2:
                    variant.Vector2Value = EditorGUI.Vector2Field(r1, GUIContent.none, variant.Vector2Value);
                    break;

                case VariantType.Vector3:
                    variant.Vector3Value = EditorGUI.Vector3Field(r1, GUIContent.none, variant.Vector3Value);
                    break;

                case VariantType.Vector4:
                    variant.Vector4Value = EditorGUI.Vector4Field(r1, (string)null, variant.Vector4Value);
                    break;

                case VariantType.Quaternion:
                    variant.QuaternionValue = SPEditorGUI.QuaternionField(r1, GUIContent.none, variant.QuaternionValue);
                    break;

                case VariantType.Color:
                    variant.ColorValue = EditorGUI.ColorField(r1, variant.ColorValue);
                    break;

                case VariantType.DateTime:
                    variant.DateValue = ConvertUtil.ToDate(EditorGUI.TextField(r1, variant.DateValue.ToString()));
                    break;

                case VariantType.GameObject:
                    variant.GameObjectValue = EditorGUI.ObjectField(r1, variant.GameObjectValue, typeof(GameObject), true) as GameObject;
                    break;

                case VariantType.Component:
                {
                    _selectComponentDrawer.AllowNonComponents = false;
                    _selectComponentDrawer.RestrictionType    = _forcedObjectType;
                    _selectComponentDrawer.ShowXButton        = true;
                    var targProp = property.FindPropertyRelative("_unityObjectReference");
                    EditorGUI.BeginChangeCheck();
                    _selectComponentDrawer.OnGUI(r1, targProp);
                    if (EditorGUI.EndChangeCheck())
                    {
                        variant.ComponentValue = targProp.objectReferenceValue as Component;
                    }
                }
                break;

                case VariantType.Object:
                {
                    var obj = variant.ObjectValue;
                    if (ComponentUtil.IsAcceptableComponentType(_forcedObjectType))
                    {
                        if (obj is GameObject || obj is Component)
                        {
                            _selectComponentDrawer.AllowNonComponents = false;
                            _selectComponentDrawer.RestrictionType    = _forcedObjectType;
                            _selectComponentDrawer.ShowXButton        = true;
                            var targProp = property.FindPropertyRelative("_unityObjectReference");
                            EditorGUI.BeginChangeCheck();
                            _selectComponentDrawer.OnGUI(r1, targProp);
                            if (EditorGUI.EndChangeCheck())
                            {
                                variant.ObjectValue = targProp.objectReferenceValue as Component;
                            }
                        }
                        else
                        {
                            EditorGUI.BeginChangeCheck();
                            obj = EditorGUI.ObjectField(r1, obj, typeof(UnityEngine.Object), true);
                            if (EditorGUI.EndChangeCheck())
                            {
                                if (obj == null)
                                {
                                    variant.ObjectValue = null;
                                }
                                else if (TypeUtil.IsType(obj.GetType(), _forcedObjectType))
                                {
                                    variant.ObjectValue = obj;
                                }
                                else
                                {
                                    var go = GameObjectUtil.GetGameObjectFromSource(obj);
                                    if (go != null)
                                    {
                                        variant.ObjectValue = go.GetComponent(_forcedObjectType);
                                    }
                                    else
                                    {
                                        variant.ObjectValue = null;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        variant.ObjectValue = EditorGUI.ObjectField(r1, obj, _forcedObjectType, true);
                    }
                }
                break;

                case VariantType.LayerMask:
                {
                    variant.LayerMaskValue = SPEditorGUI.LayerMaskField(r1, GUIContent.none, (int)variant.LayerMaskValue);
                }
                break;

                case VariantType.Rect:
                {
                    variant.RectValue = EditorGUI.RectField(r1, variant.RectValue);
                }
                break;
                }
            }
        }
Beispiel #26
0
 private void DrawObjectRefField(Rect position, SerializedProperty property)
 {
     if (ComponentUtil.IsAcceptableComponentType(_restrictionType))
     {
         var fieldObjType = (!this.SearchChildren && TypeUtil.IsType(_restrictionType, typeof(UnityEngine.Component))) ? _restrictionType : typeof(UnityEngine.GameObject);
         var obj          = EditorGUI.ObjectField(position, property.objectReferenceValue, fieldObjType, this.AllowSceneObject);
         if (this.ForceOnlySelf)
         {
             var targGo = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
             var ngo    = GameObjectUtil.GetGameObjectFromSource(obj);
             if (targGo == ngo ||
                 (this.SearchChildren && targGo.IsParentOf(ngo)))
             {
                 //property.objectReferenceValue = obj;
                 var o = obj;
                 if (this.SearchChildren && o == null)
                 {
                     o = ngo.GetComponentInChildren(_restrictionType);
                 }
                 property.objectReferenceValue = o;
             }
         }
         else
         {
             //property.objectReferenceValue = obj;
             //property.objectReferenceValue = ObjUtil.GetAsFromSource(_restrictionType, obj) as UnityEngine.Object;
             var o = ObjUtil.GetAsFromSource(_restrictionType, obj) as UnityEngine.Object;
             if (this.SearchChildren && o == null && GameObjectUtil.GetGameObjectFromSource(obj) != null)
             {
                 o = GameObjectUtil.GetGameObjectFromSource(obj).GetComponentInChildren(_restrictionType);
             }
             property.objectReferenceValue = o;
         }
     }
     else if (this.AllowNonComponents)
     {
         var fieldObjType = (TypeUtil.IsType(_restrictionType, typeof(UnityEngine.Object))) ? _restrictionType : typeof(UnityEngine.Object);
         var obj          = EditorGUI.ObjectField(position, property.objectReferenceValue, fieldObjType, this.AllowSceneObject);
         if (this.ForceOnlySelf)
         {
             var targGo = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
             var ngo    = GameObjectUtil.GetGameObjectFromSource(obj);
             if (targGo == ngo ||
                 (this.SearchChildren && targGo.IsParentOf(ngo)))
             {
                 //property.objectReferenceValue = obj;
                 //property.objectReferenceValue = ObjUtil.GetAsFromSource(_restrictionType, obj) as UnityEngine.Object;
                 var o = ObjUtil.GetAsFromSource(_restrictionType, obj) as UnityEngine.Object;
                 if (this.SearchChildren && o == null)
                 {
                     o = ngo.GetComponentInChildren(_restrictionType);
                 }
                 property.objectReferenceValue = o;
             }
         }
         else
         {
             //property.objectReferenceValue = obj;
             //property.objectReferenceValue = ObjUtil.GetAsFromSource(_restrictionType, obj) as UnityEngine.Object;
             var o = ObjUtil.GetAsFromSource(_restrictionType, obj) as UnityEngine.Object;
             if (this.SearchChildren && o == null && GameObjectUtil.GetGameObjectFromSource(obj) != null)
             {
                 o = GameObjectUtil.GetGameObjectFromSource(obj).GetComponentInChildren(_restrictionType);
             }
             property.objectReferenceValue = o;
         }
     }
     else
     {
         var ogo = GameObjectUtil.GetGameObjectFromSource(property.objectReferenceValue);
         var ngo = EditorGUI.ObjectField(position, ogo, typeof(GameObject), this.AllowSceneObject) as GameObject;
         if (ogo != ngo)
         {
             if (this.ForceOnlySelf)
             {
                 var targGo = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                 if (targGo == ngo ||
                     (this.SearchChildren && targGo.IsParentOf(ngo)))
                 {
                     //property.objectReferenceValue = ngo.GetComponent(_restrictionType);
                     //property.objectReferenceValue = ObjUtil.GetAsFromSource(_restrictionType, ngo) as UnityEngine.Object;
                     var o = ObjUtil.GetAsFromSource(_restrictionType, ngo) as UnityEngine.Object;
                     if (this.SearchChildren && o == null)
                     {
                         o = ngo.GetComponentInChildren(_restrictionType);
                     }
                     property.objectReferenceValue = o;
                 }
             }
             else
             {
                 //property.objectReferenceValue = (ngo == null) ? null : ngo.GetComponent(_restrictionType);
                 //property.objectReferenceValue = ObjUtil.GetAsFromSource(_restrictionType, ngo) as UnityEngine.Object;
                 var o = ObjUtil.GetAsFromSource(_restrictionType, ngo) as UnityEngine.Object;
                 if (this.SearchChildren && o == null)
                 {
                     o = ngo.GetComponentInChildren(_restrictionType);
                 }
                 property.objectReferenceValue = o;
             }
         }
     }
 }
Beispiel #27
0
        public void OnGUI(Rect position, SerializedProperty property)
        {
            //if (property.propertyType != SerializedPropertyType.ObjectReference || !TypeUtil.IsType(_restrictionType, typeof(Component), typeof(IComponent)))
            if (property.propertyType != SerializedPropertyType.ObjectReference || (!this.AllowNonComponents && !(TypeUtil.IsType(_restrictionType, typeof(Component)) || _restrictionType.IsInterface)))
            {
                this.DrawAsMismatchedAttribute(position, property);
                return;
            }

            this.Init();

            GameObject targGo;

            if (this.ForceOnlySelf)
            {
                targGo = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (targGo == null)
                {
                    this.DrawAsMismatchedAttribute(position, property);
                    return;
                }

                if (property.objectReferenceValue == null)
                {
                    property.objectReferenceValue = targGo.GetComponent(_restrictionType);
                }
            }

            targGo = GameObjectUtil.GetGameObjectFromSource(property.objectReferenceValue);
            if (property.objectReferenceValue == null)
            {
                //SPEditorGUI.DefaultPropertyField(position, property, label);
                if (!this.ForceOnlySelf)
                {
                    this.DrawObjectRefField(position, property);
                }
                else
                {
                    EditorGUI.LabelField(position, "Malformed serializable field.");
                }
            }
            else if (this.AllowNonComponents)
            {
                if (targGo == null)
                {
                    this.DrawObjectRefField(position, property);
                }
                else
                {
                    this.ChoiceSelector.BeforeGUI(this, property, this.ComponentRestrictionType);
                    var components = this.ChoiceSelector.GetComponents();

                    var fullsize = position;
                    if (components.Length == 0 ||
                        (this.ShowXButton && SPEditorGUI.XButton(ref position, "Clear Selected Object", this.XButtonOnRightSide)))
                    {
                        property.objectReferenceValue = null;
                        fullsize = this.DrawDotDotButton(fullsize, property);
                        this.DrawObjectRefField(fullsize, property);

                        this.ChoiceSelector.GUIComplete(property, -1);
                    }
                    else
                    {
                        position = this.DrawDotDotButton(position, property);
                        var names = this.ChoiceSelector.GetPopupEntries();
                        System.Array.Resize(ref names, names.Length + 1);
                        names[names.Length - 1] = EditorHelper.TempContent(targGo.name + " (...GameObject)");

                        int oi = (property.objectReferenceValue is GameObject) ? names.Length - 1 : this.ChoiceSelector.GetPopupIndexOfComponent(property.objectReferenceValue as Component);
                        int ni = EditorGUI.Popup(position, oi, names);

                        if (oi != ni)
                        {
                            if (ni == names.Length - 1)
                            {
                                property.objectReferenceValue = targGo;
                            }
                            else
                            {
                                property.objectReferenceValue = this.ChoiceSelector.GetComponentAtPopupIndex(ni);
                            }

                            //if (ni < components.Length)
                            //    property.objectReferenceValue = this.ChoiceSelector.GetComponentAtPopupIndex(ni);
                            //else
                            //    property.objectReferenceValue = targGo;
                        }

                        this.ChoiceSelector.GUIComplete(property, ni);
                    }
                }
            }
            else
            {
                this.ChoiceSelector.BeforeGUI(this, property, this.ComponentRestrictionType);
                var components = this.ChoiceSelector.GetComponents();

                var fullsize = position;
                if (components.Length == 0 ||
                    (this.ShowXButton && SPEditorGUI.XButton(ref position, "Clear Selected Object", this.XButtonOnRightSide)))
                {
                    property.objectReferenceValue = null;
                    fullsize = this.DrawDotDotButton(fullsize, property);
                    this.DrawObjectRefField(fullsize, property);

                    this.ChoiceSelector.GUIComplete(property, -1);
                }
                else
                {
                    position = this.DrawDotDotButton(position, property);
                    var names = this.ChoiceSelector.GetPopupEntries();
                    int oi    = this.ChoiceSelector.GetPopupIndexOfComponent(property.objectReferenceValue as Component);
                    int ni    = EditorGUI.Popup(position, oi, names);
                    if (oi != ni)
                    {
                        property.objectReferenceValue = this.ChoiceSelector.GetComponentAtPopupIndex(ni);
                    }

                    this.ChoiceSelector.GUIComplete(property, ni);
                }
            }
        }
        private object ResolveTargetAnimator(object arg)
        {
            var obj = _targetAnimator.GetTarget <UnityEngine.Object>(arg);

            ISPAnimationSource src    = null;
            ISPAnimator        spanim = null;
            Animation          anim   = null;

            if (ObjUtil.GetAsFromSource <ISPAnimationSource>(obj, out src))
            {
                return(src);
            }
            if (ObjUtil.GetAsFromSource <ISPAnimator>(obj, out spanim))
            {
                return(spanim);
            }
            if (ObjUtil.GetAsFromSource <Animation>(obj, out anim))
            {
                return(anim);
            }

            if (_targetAnimator.SearchesScene || _targetAnimator.TargetsTriggerArg)
            {
                var go = GameObjectUtil.FindRoot(GameObjectUtil.GetGameObjectFromSource(obj));
                if (go == null)
                {
                    return(null);
                }

                SPAnimationController spcont;
                if (go.FindComponent <SPAnimationController>(out spcont))
                {
                    return(spcont);
                }

                if (go.FindComponent <Animation>(out anim))
                {
                    return(anim);
                }
            }

            /*
             * if(obj == null || obj is ISPAnimationSource || obj is ISPAnimator || obj is Animation)
             * {
             *  return obj;
             * }
             * else if (_targetAnimator.Find != TriggerableTargetObject.FindCommand.Direct || _targetAnimator.TargetsTriggerArg)
             * {
             *  var go = GameObjectUtil.FindRoot(GameObjectUtil.GetGameObjectFromSource(obj));
             *  if (go == null) return null;
             *
             *  SPAnimationController spcont;
             *  if (go.FindComponent<SPAnimationController>(out spcont))
             *      return spcont;
             *
             *  Animation anim;
             *  if (go.FindComponent<Animation>(out anim))
             *      return anim;
             * }
             */

            return(null);
        }
Beispiel #29
0
        private static void ApplyDefaultAsList(SerializedProperty property, System.Type elementType, System.Type restrictionType, EntityRelativity relativity)
        {
            if (property.arraySize != 0)
            {
                return;
            }
            if (elementType == null || restrictionType == null)
            {
                return;
            }

            if (TypeUtil.IsType(elementType, typeof(VariantReference)))
            {
                var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (object.ReferenceEquals(targ, null))
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject);
                    if (obj != null)
                    {
                        property.arraySize = 1;
                        var variant = EditorHelper.GetTargetObjectOfProperty(property.GetArrayElementAtIndex(0)) as VariantReference;
                        if (variant == null)
                        {
                            return;
                        }
                        variant.Value = obj;
                        property.serializedObject.Update();
                        GUI.changed = true;
                    }
                    else if (property.arraySize > 0)
                    {
                        property.arraySize = 0;
                        GUI.changed        = true;
                    }
                    return;
                }

                switch (relativity)
                {
                case EntityRelativity.Entity:
                {
                    targ = targ.FindRoot();
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, true);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        var variant = EditorHelper.GetTargetObjectOfProperty(property.GetArrayElementAtIndex(i)) as VariantReference;
                        if (variant != null)
                        {
                            variant.Value = arr[i];
                        }
                    }
                    property.serializedObject.Update();
                    GUI.changed = true;
                }
                break;

                case EntityRelativity.Self:
                {
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, false);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        var variant = EditorHelper.GetTargetObjectOfProperty(property.GetArrayElementAtIndex(i)) as VariantReference;
                        if (variant != null)
                        {
                            variant.Value = arr[i];
                        }
                    }
                    property.serializedObject.Update();
                    GUI.changed = true;
                }
                break;

                case EntityRelativity.SelfAndChildren:
                {
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, true);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        var variant = EditorHelper.GetTargetObjectOfProperty(property.GetArrayElementAtIndex(i)) as VariantReference;
                        if (variant != null)
                        {
                            variant.Value = arr[i];
                        }
                    }
                    property.serializedObject.Update();
                    GUI.changed = true;
                }
                break;
                }
            }
            else if (TypeUtil.IsType(elementType, typeof(UnityEngine.Object)))
            {
                var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (object.ReferenceEquals(targ, null))
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject) as UnityEngine.Object;
                    if (obj != null)
                    {
                        property.arraySize = 1;
                        property.GetArrayElementAtIndex(0).objectReferenceValue = obj;
                        GUI.changed = true;
                    }
                    else if (property.arraySize > 0)
                    {
                        property.arraySize = 0;
                        GUI.changed        = true;
                    }
                    return;
                }

                switch (relativity)
                {
                case EntityRelativity.Entity:
                {
                    targ = targ.FindRoot();
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, true);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        property.GetArrayElementAtIndex(i).objectReferenceValue = arr[i] as UnityEngine.Object;
                    }
                }
                break;

                case EntityRelativity.Self:
                {
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, false);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        property.GetArrayElementAtIndex(i).objectReferenceValue = arr[i] as UnityEngine.Object;
                    }
                }
                break;

                case EntityRelativity.SelfAndChildren:
                {
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, true);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        property.GetArrayElementAtIndex(i).objectReferenceValue = arr[i] as UnityEngine.Object;
                    }
                }
                break;
                }
            }
        }
        private static void ApplyDefaultAsSingle(SerializedProperty property, System.Type fieldType, System.Type restrictionType, EntityRelativity relativity)
        {
            if (fieldType == null)
            {
                return;
            }

            if (TypeUtil.IsType(fieldType, typeof(VariantReference)))
            {
                var variant = EditorHelper.GetTargetObjectOfProperty(property) as VariantReference;
                if (variant == null)
                {
                    return;
                }
                if (variant.Value != null)
                {
                    return;
                }

                var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (targ == null)
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject);
                    if (obj != null)
                    {
                        variant.Value = obj;
                        property.serializedObject.Update();
                        GUI.changed = true;
                    }
                    return;
                }

                switch (relativity)
                {
                case EntityRelativity.Entity:
                {
                    targ = targ.FindRoot();

                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ);
                    if (obj == null && ComponentUtil.IsAcceptableComponentType(restrictionType))
                    {
                        obj = targ.GetComponentInChildren(restrictionType);
                    }
                    if (obj != null)
                    {
                        variant.Value = obj;
                        property.serializedObject.Update();
                    }
                }
                break;

                case EntityRelativity.Self:
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ);
                    if (obj != null)
                    {
                        variant.Value = obj;
                        property.serializedObject.Update();
                    }
                }
                break;

                case EntityRelativity.SelfAndChildren:
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ);
                    if (obj == null && ComponentUtil.IsAcceptableComponentType(restrictionType))
                    {
                        obj = targ.GetComponentInChildren(restrictionType);
                    }
                    if (obj != null)
                    {
                        variant.Value = obj;
                        property.serializedObject.Update();
                    }
                }
                break;
                }
            }
            else if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                if (property.objectReferenceValue != null)
                {
                    return;
                }

                var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (targ == null)
                {
                    property.objectReferenceValue = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject) as UnityEngine.Object;
                    return;
                }

                switch (relativity)
                {
                case EntityRelativity.Entity:
                {
                    targ = targ.FindRoot();

                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ) as UnityEngine.Object;
                    if (obj == null && ComponentUtil.IsAcceptableComponentType(restrictionType))
                    {
                        obj = targ.GetComponentInChildren(restrictionType);
                    }
                    if (obj != null)
                    {
                        property.objectReferenceValue = obj;
                        GUI.changed = true;
                    }
                }
                break;

                case EntityRelativity.Self:
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ) as UnityEngine.Object;
                    if (obj != null)
                    {
                        property.objectReferenceValue = obj;
                        GUI.changed = true;
                    }
                }
                break;

                case EntityRelativity.SelfAndChildren:
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ) as UnityEngine.Object;
                    if (obj == null && ComponentUtil.IsAcceptableComponentType(restrictionType))
                    {
                        obj = targ.GetComponentInChildren(restrictionType);
                    }
                    if (obj != null)
                    {
                        property.objectReferenceValue = obj;
                        GUI.changed = true;
                    }
                }
                break;
                }
            }
        }