Pathfinding.GraphEditor.ObjectField C# (CSharp) Method

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
        public Object ObjectField(GUIContent label, Object obj, System.Type objType, bool allowSceneObjects)
        {
            obj = EditorGUILayout.ObjectField (label, obj, objType, allowSceneObjects);

            if (obj != null) {
                if (allowSceneObjects && !EditorUtility.IsPersistent (obj)) {
                    //Object is in the scene
                    var com = obj as Component;
                    var go = obj as GameObject;
                    if (com != null) {
                        go = com.gameObject;
                    }
                    if (go != null) {
                        var urh = go.GetComponent<UnityReferenceHelper> ();
                        if (urh == null) {

                            if (FixLabel ("Object's GameObject must have a UnityReferenceHelper component attached")) {
                                go.AddComponent<UnityReferenceHelper>();
                            }
                        }
                    }

                } else if (EditorUtility.IsPersistent (obj)) {

                    string path = AssetDatabase.GetAssetPath (obj);

                    System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"Resources[/|\\][^/]*$");

                    if (!rg.IsMatch(path)) {
                        if (FixLabel ("Object must be in the 'Resources' folder, top level")) {
                            if (!System.IO.Directory.Exists (Application.dataPath+"/Resources")) {
                                System.IO.Directory.CreateDirectory (Application.dataPath+"/Resources");
                                AssetDatabase.Refresh ();
                            }
                            string ext = System.IO.Path.GetExtension(path);

                            string error = AssetDatabase.MoveAsset	(path,"Assets/Resources/"+obj.name+ext);

                            if (error == "") {
                                //Debug.Log ("Successful move");
                                path = AssetDatabase.GetAssetPath (obj);
                            } else {
                                Debug.LogError ("Couldn't move asset - "+error);
                            }
                        }
                    }

                    if (!AssetDatabase.IsMainAsset (obj) && obj.name != AssetDatabase.LoadMainAssetAtPath (path).name) {
                        if (FixLabel ("Due to technical reasons, the main asset must\nhave the same name as the referenced asset")) {
                            string error = AssetDatabase.RenameAsset (path,obj.name);
                            if (error == "") {
                                //Debug.Log ("Successful");
                            } else {
                                Debug.LogError ("Couldn't rename asset - "+error);
                            }
                        }
                    }
                }
            }

            return obj;
        }

Same methods

GraphEditor::ObjectField ( string label, Object obj, System objType, bool allowSceneObjects ) : Object

Usage Example

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--;
                }
            }
        }