Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果两个脚本未连接到任何游戏对象,我如何从另一个脚本访问脚本?_C#_Unity3d - Fatal编程技术网

C# 如果两个脚本未连接到任何游戏对象,我如何从另一个脚本访问脚本?

C# 如果两个脚本未连接到任何游戏对象,我如何从另一个脚本访问脚本?,c#,unity3d,C#,Unity3d,第一个脚本是EditorWindow:并放置在编辑器文件夹中: public class HierarchyEditor : EditorWindow 第二个脚本是MonoBehavior,但带有attibute:[InitializeOnLoad] using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEditor; using UnityE

第一个脚本是EditorWindow:并放置在编辑器文件夹中:

public class HierarchyEditor : EditorWindow
第二个脚本是MonoBehavior,但带有attibute:[InitializeOnLoad]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class CustomHierarchy : MonoBehaviour
{
    private static Vector2 offset = new Vector2(0, 2);
    public static Color gameObjectFontColor = Color.black;
    public static Color prefabOrgFontColor = Color.black;
    public static Color prefabModFontColor = Color.white;
    public static Color inActiveColor = new Color(0.01f, 0.4f, 0.25f);
    public static Color meshRendererColor = Color.yellow;

    static CustomHierarchy()
    {
        EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
    }
    private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
    {
        Color fontColor = gameObjectFontColor;
        Color backgroundColor = new Color(.76f, .76f, .76f);
        FontStyle styleFont = FontStyle.Normal;
        var obj = EditorUtility.InstanceIDToObject(instanceID);
        GameObject gameObj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

        if (Selection.instanceIDs.Contains(instanceID))
        {
            backgroundColor = new Color(0.24f, 0.48f, 0.90f);
        }
        if (obj != null)
        {
            var prefabType = PrefabUtility.GetPrefabType(obj);
            if (gameObj.activeInHierarchy == false)
            {
                backgroundColor = inActiveColor;
            }

            if (prefabType == PrefabType.PrefabInstance)
            {
                styleFont = FontStyle.Bold;
                PropertyModification[] prefabMods = PrefabUtility.GetPropertyModifications(obj);
                foreach (PropertyModification prefabMod in prefabMods)
                {
                    if (prefabMod.propertyPath.ToString() != "m_Name" && prefabMod.propertyPath.ToString() != "m_LocalPosition.x" && prefabMod.propertyPath.ToString() != "m_LocalPosition.y" && prefabMod.propertyPath.ToString() != "m_LocalPosition.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.x" && prefabMod.propertyPath.ToString() != "m_LocalRotation.y" && prefabMod.propertyPath.ToString() != "m_LocalRotation.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.w" && prefabMod.propertyPath.ToString() != "m_RootOrder" && prefabMod.propertyPath.ToString() != "m_IsActive")
                    {
                        if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
                        {
                            fontColor = meshRendererColor;
                        }
                        else
                        {
                            fontColor = prefabModFontColor;
                        }

                        break;
                    }
                }
                if (fontColor != prefabModFontColor)
                {
                    if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
                    {
                        fontColor = meshRendererColor;
                    }
                    else
                    {
                        fontColor = prefabOrgFontColor;
                    }
                }
            }
            else
            {
                if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
                {
                    fontColor = meshRendererColor;
                }
            }
            Rect offsetRect = new Rect(selectionRect.position + offset, selectionRect.size);
            EditorGUI.DrawRect(selectionRect, backgroundColor);
            EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle()
            {
                normal = new GUIStyleState() { textColor = fontColor },
                fontStyle = styleFont
            }
            );
        }
    }

    public static bool HasAllComponents(GameObject gameObject, params System.Type[] types)
    {
        for (int i = 0; i < types.Length; i++)
        {
            if (gameObject.GetComponent(types[i]) == null)
                return false;
        }

        return true;
    }
}
这是我想在编辑器窗口脚本中获取并显示的游戏对象。
在CustomHierarchy脚本中,我用黄色fontColor=MeshRenderColor为对象上色,但我也希望在编辑器窗口中显示这些对象。

由于
CustomHierarchy
类的所有成员都是静态的,您也可以简单地将类设置为静态的:

public static class CustomHierarchy
{
    // ...
}
HierarchyEditor
只需访问您的成员即可:

public class HierarchyEditor : EditorWindow
{
    void Test()
    {
        Color someColor = CustomHierarchy.gameObjectFontColor;
        // ...
    }

    // ...
}

由于
CustomHierarchy
类的所有成员都是静态的,因此您也可以简单地将类设置为静态的:

public static class CustomHierarchy
{
    // ...
}
HierarchyEditor
只需访问您的成员即可:

public class HierarchyEditor : EditorWindow
{
    void Test()
    {
        Color someColor = CustomHierarchy.gameObjectFontColor;
        // ...
    }

    // ...
}

如果您不打算将
CustomHierarchy
附加到任何游戏对象,为什么要从
monobhavior
派生它?
CustomHierarchy
是要在编辑器中使用还是在游戏中使用?@ThomasHilbert在CustomHierarchy中I在层次结构中以黄色显示特定于游戏对象的颜色。这是我想在编辑器窗口中显示的游戏对象。因此,在像现在这样的层次结构中,我将以黄色显示它们,并将它们显示在编辑器窗口中。在脚本CustomHierarchy中,我所做的每一个地方,如果(HasAllComponents(gameObj,typeof(MeshRenderer),typeof(BoxCollider)),那么在脚本中,我希望这个游戏对象能够获得并显示在编辑器窗口中。@ThomasHilbert我已经用CustomHierarchy编辑了我的问题。我想在editorwindow中操作黄色的游戏对象,例如在editorwindow中,以显示customhierarchy中所有黄色游戏对象的列表。
MonoBehavior
的唯一目的是赋予类附加到游戏对象的能力,并让Unity调用其
Update
等方法。据我所知,您可以删除
monobhavior
基类,让一切都正常工作。如果您不打算将
CustomHierarchy
附加到任何游戏对象,为什么要从
monobhavior
派生它?
CustomHierarchy
是要在编辑器中使用还是在游戏中使用?@ThomasHilbert在CustomHierarchy中I在层次结构中以黄色显示特定于游戏对象的颜色。这是我想在编辑器窗口中显示的游戏对象。因此,在像现在这样的层次结构中,我将以黄色显示它们,并将它们显示在编辑器窗口中。在脚本CustomHierarchy中,我所做的每一个地方,如果(HasAllComponents(gameObj,typeof(MeshRenderer),typeof(BoxCollider)),那么在脚本中,我希望这个游戏对象能够获得并显示在编辑器窗口中。@ThomasHilbert我已经用CustomHierarchy编辑了我的问题。我想在editorwindow中操作黄色的游戏对象,例如在editorwindow中,以显示customhierarchy中所有黄色游戏对象的列表。
MonoBehavior
的唯一目的是赋予类附加到游戏对象的能力,并让Unity调用其
Update
等方法。据我所知,您可以删除
monobhavior
基类,让一切都正常工作。