Jump to content
Unity Insider Forum

Inspektor Skript Logo


Zer0Cool

Recommended Posts

da ich eigene Inspektoren für jedes Skript nicht mag, habe ich einmal einen Dekorator erstellt, den man in jedes Skript sehr leicht einbauen kann.
Die Klassen sind bewusst einfach gehalten und enthalten nur das absolut notwendigste.

Beispielanwendung:

public class Example : MonoBehaviour
{
   [Logo(64, 16, "Assets/Textures/myLogo.png", true)]
   ...
   ...
}

Der 1. Parameter definiert die Höhe der Textur, der 2. das Spacing über und unter der Texture (16 / 2) und der 3. Parameter den Pfad zum Texturlogo.
Der 4. Parameter bestimmt, ob das Logo noch einmal in einen Rahmen verpackt wird.

Ergebnis:

LELGi8b.png


Hier die Klassen:

LogoAttribute.cs (kann beliebig im Projektfolder platziert werden)

using UnityEngine;
using UnityEditor;

/// <summary>
/// This class defines the LogoAttribute, so that it can be used in your regular MonoBehaviour scripts.
/// <para>
/// (c) by Zer0Cool for the Unity Insider Forum (forum.unity-community.de)
/// Skype: zer0f0rce
/// Discord: zer0f0rce #8769
/// </para>
/// 
/// Usage:
/// [LogoAttribute(64, 16, "Assets/Textures/mylogo.png", false)]
/// 
/// Hint:
/// This class is used together with <see cref="LogoDrawer"/>.   
/// </summary>
public class LogoAttribute : PropertyAttribute
{
    public Texture2D texture;
    public string assetLogoPath;
    public int height;
    public int padding;
    public bool border;

    public LogoAttribute(int height, int padding, string assetLogoPath, bool border)
    {
        this.assetLogoPath = assetLogoPath;
        this.height = height;
        this.padding = padding;
        this.border = border;
        this.texture = (Texture2D)AssetDatabase.LoadAssetAtPath(assetLogoPath, typeof(Texture));
    }
}


LogoDrawer.cs (muss innerhalb des Projektfolders in einem Editorverzeichnis platziert werden)
 

using UnityEngine;
using UnityEditor;

/// <summary>
/// This class decorates a MonoBehaviour script with a logo.
/// <para>
/// Defines how the <see cref="LogoAttribute"/> should be drawn in the inspector, when inspecting a GameObject with a MonoBehaviour which uses it.
/// </para>
/// <para>
/// (c) by Zer0Cool for the Unity Insider Forum (forum.unity-community.de)
/// Skype: zer0f0rce
/// Discord: zer0f0rce #8769
/// </para>
/// 
/// Usage:
/// [LogoAttribute(64, 16, "Assets/Textures/mylogo.png", false)]
/// 
/// Hint:
/// This class is used together with <see cref="LogoAttribute"/> and has to be placed inside an editor folder.   
/// </summary>
[CustomPropertyDrawer(typeof(LogoAttribute))]
public class LogoDrawer : DecoratorDrawer
{

    private static GUIStyle s_TempStyle = new GUIStyle();

    LogoAttribute logoAttribute
    {
        get { return ((LogoAttribute)attribute); }
    }

    public override float GetHeight()
    {
        float _height;
        if (logoAttribute.border) _height = logoAttribute.height + logoAttribute.padding * 2 + logoAttribute.padding / 2;
        else _height = logoAttribute.height + logoAttribute.padding * 2;

        return _height;
    }

    public override void OnGUI(Rect position)
    {
        // if this is not a repain or the property is null exit now
        if (Event.current.type != EventType.Repaint || logoAttribute.texture == null)
            return;

        //create object field for the sprite
        Rect spriteRect;
        spriteRect = new Rect(position.x, position.y, position.width, logoAttribute.height);
        spriteRect.width = position.width - EditorGUIUtility.singleLineHeight / 2;
        spriteRect.y += logoAttribute.padding;

        s_TempStyle.normal.background = logoAttribute.texture;
        if (logoAttribute.border) GUI.Box(new Rect(position.x, position.y + logoAttribute.padding / 3, position.width, 3), GUIContent.none);
        s_TempStyle.Draw(spriteRect, GUIContent.none, false, false, false, false);
        if (logoAttribute.border) GUI.Box(new Rect(position.x, position.y + GetHeight() - logoAttribute.padding, position.width, 3), GUIContent.none);
    }
}

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

Es gibt eine Klasse die nur diese Striche macht, die ist aber nicht von mir:

Beispielanwendung:

public class Example : MonoBehaviour
{
   [HorizontalLine]
   ...
   ...
}


HorizontalLineAttribute.cs

using UnityEngine;

[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class HorizontalLineAttribute : PropertyAttribute
{
}


HorizontalLineDrawer.cs

using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(HorizontalLineAttribute))]
public class HorizontalLineDrawer : DecoratorDrawer
{
    public override float GetHeight()
    {
        return 11;
    }

    public override void OnGUI(Rect position)
    {
        GUI.Box(new Rect(position.x, position.y + 4, position.width, 3), GUIContent.none);
    }
}

Leider vertragen sich beide Drawer nicht, daher habe ich meine obere Klasse noch einmal erweitert, sie oben.

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...