ObjectField() public method

public ObjectField ( GUIContent label, Object obj, System objType, bool allowSceneObjects ) : Object
label UnityEngine.GUIContent
obj Object
objType System
allowSceneObjects bool
return Object
Ejemplo n.º 1
0
        public void OnInspectorGUI(GridGraph graph, GridGraphRule rule)
        {
            var target = rule as RuleTexture;

            target.texture = GraphEditor.ObjectField(new GUIContent("Texture"), target.texture, typeof(Texture2D), false, true) as Texture2D;

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Generate Reference"))
            {
                SaveReferenceTexture(graph);
                EditorUtility.DisplayDialog("Reference texture saved", "A texture has been saved in which every pixel corresponds to one node. The red channel represents if a node is walkable or not. The green channel represents the (normalized) Y coordinate of the nodes.", "Ok");
            }
            GUILayout.EndHorizontal();

            if (target.texture != null)
            {
                string path = AssetDatabase.GetAssetPath(target.texture);

                if (path != "")
                {
                    var importer = AssetImporter.GetAtPath(path) as TextureImporter;
                    if (importer != null && !importer.isReadable)
                    {
                        if (GraphEditor.FixLabel("Texture is not readable"))
                        {
                            importer.isReadable = true;
                            EditorUtility.SetDirty(importer);
                            AssetDatabase.ImportAsset(path);
                        }
                    }
                }
            }

            target.scalingMode = (RuleTexture.ScalingMode)EditorGUILayout.EnumPopup("Scaling Mode", target.scalingMode);
            if (target.scalingMode == RuleTexture.ScalingMode.FixedScale)
            {
                EditorGUI.indentLevel++;
                target.nodesPerPixel = EditorGUILayout.FloatField("Nodes Per Pixel", target.nodesPerPixel);
                EditorGUI.indentLevel--;
            }

            for (int i = 0; i < 4; i++)
            {
                char channelName = "RGBA"[i];
                target.channels[i] = (RuleTexture.ChannelUse)EditorGUILayout.Popup("" + channelName, (int)target.channels[i], ChannelUseNames);

                if (target.channels[i] != RuleTexture.ChannelUse.None)
                {
                    EditorGUI.indentLevel++;
                    if (target.channels[i] != RuleTexture.ChannelUse.Walkable)
                    {
                        target.channelScales[i] = EditorGUILayout.FloatField("Scale", target.channelScales[i]);
                    }

                    string help = "";
                    switch (target.channels[i])
                    {
                    case RuleTexture.ChannelUse.Penalty:
                        help = "Penalty goes from 0 to " + target.channelScales[i].ToString("0") + " depending on the " + channelName + " channel value";
                        break;

                    case RuleTexture.ChannelUse.Position:
                        help = "Nodes will have their Y coordinate set to a value between 0 and " + target.channelScales[i].ToString("0") + " depending on the " + channelName + " channel";

                        if (graph.collision.heightCheck)
                        {
                            EditorGUILayout.HelpBox("Height testing is enabled but the node positions will be overwritten by the texture data. You should disable either height testing or this feature.", MessageType.Error);
                        }
                        break;

                    case RuleTexture.ChannelUse.WalkablePenalty:
                        help = "If the " + channelName + " channel is 0, the node is made unwalkable. Otherwise the penalty goes from 0 to " + target.channelScales[i].ToString("0") + " depending on the " + channelName + " channel value";
                        break;

                    case RuleTexture.ChannelUse.Walkable:
                        help = "If the " + channelName + " channel is 0, the node is made unwalkable.";
                        break;
                    }

                    EditorGUILayout.HelpBox(help, MessageType.None);

                    if ((target.channels[i] == RuleTexture.ChannelUse.Penalty || target.channels[i] == RuleTexture.ChannelUse.WalkablePenalty) && target.channelScales[i] < 0)
                    {
                        EditorGUILayout.HelpBox("Negative penalties are not supported. You can instead raise the penalty of other nodes.", MessageType.Error);
                    }

                    EditorGUI.indentLevel--;
                }
            }
        }