gpt4 book ai didi

c# - 为什么脚本仅使用渲染器组件为某些对象着色?

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:25 25 4
gpt4 key购买 nike

using System.Linq;
using UnityEditor;
using UnityEngine;
//Adapted from Unity3DCollege YouTube Video Tutorial https://www.youtube.com/watch?v=pdDrY8Mc2lU
[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);
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(gameObj.GetComponent<Renderer>() == true)
{
fontColor = Color.yellow;
}

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")
{
fontColor = prefabModFontColor;
break;
}
}
if (fontColor != prefabModFontColor) fontColor = prefabOrgFontColor;
}
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
}
);
}
}
}

我添加的部分:

if(gameObj.GetComponent<Renderer>() == true)
{
fontColor = Color.yellow;
}

但是在层次结构中,大多数带有网格渲染器的对象都是黑色的,只有一些是黄色的。

我想使用网格渲染器或渲染器组件将层次结构中的所有游戏对象涂成黄色。也许所有没有渲染器组件的游戏对象都是红色的。

最佳答案

我认为预制件的颜色正在变黑,即使它有渲染器也是因为

if (fontColor != prefabModFontColor) fontColor = prefabOrgFontColor;

也许可以尝试这样的事情

 if (fontColor != prefabModFontColor)
{
if(gameObj.GetComponent<Renderer>() == true)
fontColor = Color.yellow;
else
fontColor = prefabOrgFontColor;
}

关于c# - 为什么脚本仅使用渲染器组件为某些对象着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53031181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com