除了使用“资产/Gizmos"之外,如何具有自定义脚本图标?在Unity3D中 [英] How to have custom script icons other than using "Assets/Gizmos" in Unity3D

查看:180
本文介绍了除了使用“资产/Gizmos"之外,如何具有自定义脚本图标?在Unity3D中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题可能被问了很多次,但是通常回答是错误的.

I know this was asked a lot of times probably .. but it is very often answered wrong.

在检查器(例如,图2和图3)和ProjectView(例如,图1)中为特定的组件/脚本使用自定义图标

Use a custom icon for specific components/scripts in the Inspector (e.g. Figure 2 and Figure 3) and the ProjectView (e.g. Figure 1)

对于每个应该带有图标的组件/类,我在文件夹中都有一个Accreding图标文件

For each component/class that shall have the icon I have an accroding Icon file in the folder

Assets/Gizmos/<Path>/<To>/<Namespace>/<ClassName> icon

,然后在Import Settigns中将TextureType设置为Editor GUI and Legacy GUI

这很好..直到现在,我仍然是实现该目标的唯一方法(请记住以下部分我绝对不想要的东西).

This is working fine .. and until now the only way how I could achieve that (having in mind the below section What I definitely do NOT want).

但是,我想知道是否真的没有更好的方法来让每个脚本具有唯一的Icon文件.这使得项目/UnityPackage不必要地庞大.同样,如果我重命名一个类,我也总是必须重命名相应的图标文件...这意味着感觉不正确!

However, I wondered if there is really no better way having to have a unique Icon file for each script. This makes the project/UnityPackage unnecessarily huge. Also if I rename a class I always have to rename the according icon file as well ... This imply doesn't feel right!

大多数Unity内置行为和组件都有一个唯一的图标.但是,来自新PackageManager的外部Packages都具有内置图标,有时还有一个Gizmos文件夹,但是它没有遵循上述命名规则...因此,显然为它们配置了图标.

Most Unity build-in Behaviours and Components have a unique icon. But also external Packages coming from the new PackageManager have built-in icons and sometimes a Gizmos folder but it is not following the above naming rule ... so apparently the icon is configured somehow else for them.

因此,我的问题:
是否有更好的方法来为脚本/组件添加这些图标?

Therefore my questions:
Is there any better way to have those icons for scripts/components?

最好编写脚本并重用一个单一的图标文件,而不是在多个不同名称的文件中使用相同的图标.

Preferably scripted and reusing ONE single icon file instead of having the same icon in multiple differently named files.

和/或从PackageManager中为脚本定义的图标在哪里/如何定义?

在所有已附加这些组件的GameObjects中也在场景视图中显示图标(例如,图4).这是由于通过检查器选择此脚本的图标(如图5所示)所致(如在,甚至通过 OnDrawGizmos DrawGizmo . 使用我目前在Gizmos文件夹中使用的方法不会发生这种情况!

Show the Icon also in the SceneView for all GameObjects having those components attached (e.g. Figure 4). This is caused by either selecting the icon for this script via the Inspector as in Figure 5 (as allways suggested e.g. in this post or here and even by Unity - Assign Icons ) or using OnDrawGizmos or DrawGizmo. This is not happening using the approach I use currently with the Gizmos folder!

更新

因为在此答案中提出了建议:我也知道我可以做到这一点,并通过Gizmos SceneView的设置. 但是,假设我喜欢25个不同的模块,每个模块都有不同的图标.我不想必须在每个项目的基础上逐个禁用SceneView设置中的Gizmos!甚至提供的脚本似乎也很庞大.反思将是我永远不会采取的最后手段.另外,我宁愿根本不将这些图标显示为Gizmos,而不是全部禁用它们.

Because this was suggested in this answer: I also know that I could do that and turn them off via the Gizmos settings of the SceneView. But imagine I have like 25 different modules and various different icons each. I don't want to have to disable their Gizmos in the SceneView settings one by one on a per project basis! Even the provided script seems like a vast hackaround. Reflection would be the very last resort I would ever take. Also I'ld prefer to not even have those icons appear as possible Gizmos at all instead of disabling them all.

推荐答案

您可以使用图5设置图标,然后从gizmos下拉列表中关闭该图标的gizmos.

You can set the icon with figure 5 and then turn the gizmos for that icon off from the gizmos drop down.

结合以上步骤,您可以尝试使用源自

Injunction with the step above you could try this script derived from here it uses reflection to find the class responsible for turning off the the gizmos and icons. This would execute any time your scripts recompiled to keep those icons off or if you added any new icons to the autohide icon file. Note: scriptClass will be an empty string for built in components eg.Camera, AudoSource

using UnityEditor;
using System;
using System.Reflection;

public class DisableAllGizmos
{
    [UnityEditor.Callbacks.DidReloadScripts]
    private static void OnScriptsReloaded()
    {
        var Annotation = Type.GetType("UnityEditor.Annotation, UnityEditor");
        var ClassId = Annotation.GetField("classID");
        var ScriptClass = Annotation.GetField("scriptClass");
        var Flags = Annotation.GetField("flags");
        var IconEnabled = Annotation.GetField("iconEnabled");

        Type AnnotationUtility = Type.GetType("UnityEditor.AnnotationUtility, UnityEditor");
        var GetAnnotations = AnnotationUtility.GetMethod("GetAnnotations", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
        var SetIconEnabled = AnnotationUtility.GetMethod("SetIconEnabled", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

        Array annotations = (Array)GetAnnotations.Invoke(null, null);
        foreach (var a in annotations)
        {
            int classId = (int)ClassId.GetValue(a);
            string scriptClass = (string)ScriptClass.GetValue(a);
            int flags = (int)Flags.GetValue(a);
            int iconEnabled = (int)IconEnabled.GetValue(a);

            // this is done to ignore any built in types
            if (string.IsNullOrEmpty(scriptClass))
            {
                continue;
            }

            // load a json or text file with class names

            const int HasIcon = 1;
            bool hasIconFlag = (flags & HasIcon) == HasIcon;

            // Added for refrence
            //const int HasGizmo = 2;
            //bool hasGizmoFlag = (flags & HasGizmo) == HasGizmo;

            if (/*Compare class names in file to scriptClass == true*/)
            {
                if (hasIconFlag && (iconEnabled != 0))
                {
                    UnityEngine.Debug.LogWarning(string.Format("Script:'{0}' is not ment to show its icon in the scene view and will auto hide now. " +
                        "Icon auto hide is checked on script recompile, if you'd like to change this please remove it from the config",scriptClass));
                    SetIconEnabled.Invoke(null, new object[] { classId, scriptClass, 0 });
                }
            }
        }
    }
}

  1. 显示在带有小控件的检查器中

  1. Shown in the inspector with a gizmo

从Gizmos下拉菜单中隐藏图标

Hide Icon from gizmos dropdown

图标仍显示在检查器和项目视图中,但不显示在场景中

Icon still appears in the inspector and in the project view but not in the scene

这篇关于除了使用“资产/Gizmos"之外,如何具有自定义脚本图标?在Unity3D中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆