简体   繁体   English

如果两个脚本都未附加到任何GameObject,如何从另一个脚本访问脚本?

[英]How can I access a script from another script if both scripts not attached to any GameObject?

The first script is EditorWindow: And places in Editor folder: 第一个脚本是EditorWindow:并放置在Editor文件夹中:

public class HierarchyEditor : EditorWindow

The second script is MonoBehaviour but with attibute: [InitializeOnLoad] 第二个脚本是MonoBehaviour,但带有修饰符:[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;
    }
}

I want to access some variables that are in the CustomHierarchy script from the HierarchyEditor script. 我想从HierarchyEditor脚本访问CustomHierarchy脚本中的一些变量。

In the CustomHierarchy script in all places: 在所有位置的CustomHierarchy脚本中:

if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
                    {
                        fontColor = meshRendererColor;
                    }

This gameobjects I want to get and show in the editorwindow script. 我要获取的游戏对象并在editorwindow脚本中显示。 In the CustomHierarchy script I color the objects in yellow fontColor = meshRendererColor but I also want to display this objects in the editorwindow. 在CustomHierarchy脚本中,我用黄色fontColor = meshRendererColor为对象着色,但我也想在编辑器窗口中显示该对象。

As all the members of your CustomHierarchy class are static you can simply make your class static as well: 由于CustomHierarchy类的所有成员都是静态的,因此您也可以简单地将类设为静态:

public static class CustomHierarchy
{
    // ...
}

HierarchyEditor can simply access your members: HierarchyEditor可以轻松访问您的成员:

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

    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从附加到不同游戏对象的另一个脚本调用列表? - How do I call a List from another Script that is attached to a different GameObject? 如何通过GetComponent从另一个游戏对象中的另一个脚本访问变量? - How to access a variable from another script in another gameobject through GetComponent? 如何检查脚本附加到的游戏对象以便在多个游戏对象上使用相同的脚本? - How can I check the gameobject that the script is attached to in order to use the same script on multiple gameobjects? 如何在脚本中访问对撞机的游戏对象? - How can I access a Collider's GameObject in script? 在附加的GameObject脚本中,gameObject是否与此等效? - Is gameObject equivalent to this in the script of the GameObject it is attached to? 如何找到GameObject的子代或通过脚本附加到子GameObject的脚本 - How to find child of a GameObject or the script attached to child GameObject via script 如何从非活动游戏对象访问脚本? - How to access the script from inactive gameobject? 如何从另一个脚本访问脚本中的枚举? - How can I get access of a enum in a script from another script? 如何从另一个GameObject实例化a:monobehaviour脚本 - How to instantiate a : monobehaviour script from another GameObject 如何从另一个脚本中获取游戏对象 position - How to get gameobject position from another script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM