示例#1
0
        /// <summary> Creates a new instance of the drawer or returns a reusable instance from the pool. </summary>
        /// <param name="selectedIndex"> The selected index in items. </param>
        /// <param name="items"> The item options to display in the popup menu when the field is clicked. </param>
        /// <param name="memberInfo"> LinkedMemberInfo for the field, property or parameter that the drawer represents. Can be null. </param>
        /// <param name="parent"> The parent drawer of the created drawer. Can be null. </param>
        /// <param name="label"> The prefix label. </param>
        /// <param name="readOnly"> True if control should be read only. </param>
        /// <returns> The instance, ready to be used. </returns>
        public static PopupMenuDrawer Create(int selectedIndex, string[] items, LinkedMemberInfo memberInfo, IParentDrawer parent, GUIContent label, bool readOnly)
        {
            PopupMenuDrawer result;

            if (!DrawerPool.TryGet(out result))
            {
                result = new PopupMenuDrawer();
            }
            result.Setup(selectedIndex, items, memberInfo, parent, label, readOnly);
            result.LateSetup();
            return(result);
        }
        /// <inheritdoc />
        protected override void DoBuildMembers()
        {
            var value = Value;

            if (value == null)
            {
                DrawerArrayPool.Resize(ref members, 2);
                members[0] = NullToggleDrawer.Create(OnNullToggleButtonClicked, this, ReadOnly);
                members[1] = ObjectReferenceDrawer.Create(null, Types.UnityObject, this, GUIContent.none, true, false, ReadOnly);
            }
            else
            {
                var    target    = value.Target;
                bool   hasTarget = target != null;
                Object unityObject;
                bool   isUnityObject;
                bool   isAnonymous;
                string methodName;
                Type   targetType;
                int    methodIndex;
                var    method = value.Method;

                if (hasTarget)
                {
                    targetType = target.GetType();

                    UpdateMethodOptions(targetType, true);

                    unityObject   = target as Object;
                    isUnityObject = unityObject != null;

                    methodName  = method.Name;
                    isAnonymous = methodName[0] == '<';

                    if (isAnonymous)
                    {
                        string methodOrigin = methodName.Substring(1, methodName.IndexOf('>') - 1);
                        methodName = string.Concat("Anonymous Method (", methodOrigin, ")");
                    }

                    methodIndex = Array.IndexOf(methodOptionNames, methodName);
                    if (methodIndex == -1)
                    {
                        methodOptions     = methodOptions.InsertAt(0, method);
                        methodOptionNames = methodOptionNames.InsertAt(0, methodName);
                        methodIndex       = 0;
                    }
                }
                else
                {
                    methodIndex = 0;

                    if (method == null)
                    {
                        targetType    = null;
                        methodName    = "{ }";
                        unityObject   = null;
                        isUnityObject = false;
                        isAnonymous   = false;

                        ArrayPool <MethodInfo> .Resize(ref methodOptions, 1);

                        methodOptions[0] = method;
                        ArrayPool <string> .Resize(ref methodOptionNames, 1);

                        methodOptionNames[0] = methodName;
                    }
                    else
                    {
                        targetType = method.ReflectedType;

                        UpdateMethodOptions(targetType, false);

                        methodName    = method.Name;
                        unityObject   = null;
                        isUnityObject = false;
                        isAnonymous   = methodName[0] == '<';

                        if (isAnonymous)
                        {
                            string methodOrigin = methodName.Substring(1, methodName.IndexOf('>') - 1);
                            methodName = string.Concat("Anonymous Method (", methodOrigin, ")");
                        }

                        methodIndex = Array.IndexOf(methodOptionNames, methodName);
                        if (methodIndex == -1)
                        {
                            methodOptions     = methodOptions.InsertAt(0, method);
                            methodOptionNames = methodOptionNames.InsertAt(0, methodName);
                            methodIndex       = 0;
                        }
                    }
                }

                                #if DEV_MODE && PI_ASSERTATIONS
                Debug.Assert(methodOptions.Length == methodOptionNames.Length);
                                #endif

                                #if DEV_MODE
                Debug.Log(Msg(ToString() + ".DoBuildMembers with target=", target, ", type=", targetType, ", isUnityObject=", isUnityObject, ", methodName=", methodName, ", isAnonymous=", isAnonymous + ", methodNames=", StringUtils.ToString(methodOptionNames)));
                                #endif

                if (isUnityObject)
                {
                    DrawerArrayPool.Resize(ref members, 2);
                    members[0] = ObjectReferenceDrawer.Create(unityObject, Types.UnityObject, this, GUIContentPool.Empty(), true, false, ReadOnly);
                    members[1] = PopupMenuDrawer.Create(methodIndex, methodOptionNames, null, this, GUIContentPool.Empty(), ReadOnly);
                }
                else
                {
                    DrawerArrayPool.Resize(ref members, 3);
                    members[0] = NullToggleDrawer.Create(OnNullToggleButtonClicked, this, ReadOnly);
                    members[1] = TypeDrawer.Create(targetType, null, this, GUIContentPool.Empty(), ReadOnly);
                    members[2] = PopupMenuDrawer.Create(methodIndex, methodOptionNames, null, this, GUIContentPool.Empty(), ReadOnly);
                }
            }
        }