-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] Unity 3D. 1:

, 16 2017 . 11:36 +



, ! , Unity . , , , .

, . , Unity 3D. , , , . , .


, , .






[MenuItem("Tools/Initialization Project")]

. / . , , File/Create New Asset.

.

string path //   
bool valudate //      (   )
int order //      

[MenuItem("Tools/Initialization Project", true)]
public static bool Initialization()
{
    //   ,    
    return Selection.gameObjects.Length > 0;
}

[MenuItem("Tools/Initialization Project")]
public static void Initialization()
{
    //do something...
}

, , , . , , .


,




[Range(float min, float max)]

, , . . , , , 0 1 0 100.



[Header(string title)]

, .



[Space]

.



[Tooltip(string tip)]

.



[SerializeField]

. , , .



[NonSerialized]

. . get;set; . , , , . , , .



[HiddenInInspector]

. , / SerializeField.




[ExecuteInEditMode]

. , , . .

, , transform, renderer, rectTranform .. , , , .



[RequireComponent(System.Type type)]

, . . .



[AddComponentMenu(string path)]

Components -> AddComponent. , .

, .

(CustomPropertyDrawer)


, API . .

-, - PropertyAttribute. , , .

public class IntAttribute : PropertyAttribute
{
    private string path = ;

    public IntAttribute(string path)
    {
        this.path = path;
    }
}

-, , . PropertyDrawer, CustomPropertyDrawer.

[CustomPropertyDrawer(typeof(IntAttribute ))]
public class IntAttributeDrawer : PropertyDrawer
{
}

, .

, , . , , (enum) , .

, , id -> . - , ScriptableObjecte - .

. - element 1, element 2.., , .


, , . , , , , . , , GetComponent(s), GetComponent(s)InChildren ..



public interface IResource
{
    int ID
    {
        get;
    }
 
    string Name
    {
        get;
    }
}
 
[System.Serializable]
public class Effect : IResource
{
    [SerializeField]
    private string name = ;
    [SerializeField]
    private int      id = 0;
 
    public int ID
    {
        get
        {
            return id;
        }
    }
 
    public string Name
    {
        get
        {
            return name;
        }
    }
}



public interface IContainer
{
    IResource[] Resources
    {
        get;
    }
}
 
public abstract class ResourcesContainer : MonoBehavior, IContainer
{
    public virtual IResource[] Resources
    {
        get 
        {
            return null;
        }
    }
}
 
public class EffectsContainer : ResourcesContainer 
{
    [SerializeField]
    private Effect[] effects = null;
    
    public override IResource[] Resources
    {
        get 
        {
            return effects;
        }
    }
}

, , . . .


:

[CustomPropertyDrawer(typeof(IntAttribute ))]
public class IntAttributeDrawer : PropertyDrawer
{
    protected string[]  values = null;
    protected List idents = null;
 
    protected virtual void Init(SerializedProperty property)
    {
        if (attribute != null)
        {
            IntAttribute intAttribute = (IntAttribute)attribute;
            //    null, ,  ,   
            IResource[] resources = Resources.Load(intAttribute.Path).Resources;
            values = new string[resources.Length + 1];
            idents = new List(resources.Length + 1);
           
            //     -1 
            values[0] = -1: None;
            idents.Add(-1);
            for (int i = 0; i < resources.Length; i++)
            {
                values[i+1] = resources[i].ID + :  + resources[i].Path;
                idents.Add(resources[i].ID);
            }
        }
    }
 
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (property == null)
        {
            return;
        }
 
        Init(property);
        EditorGUI.BeginProperty(position, label, property);
 
        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
 
        // Don't make child fields be indented
        int indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;
 
        // Calculate rects
        Rect pathRect = new Rect(position.x, position.y, position.width - 6, position.height);
 
        int intValue = property.intValue;
        intValue = idents[EditorGUI.Popup(pathRect, Mathf.Max(0, idents.IndexOf(intValue)), Values)];
        property.intValue = intValue;
 
        EditorGUI.indentLevel = indent;
 
        EditorGUI.EndProperty();
    }
}

   ScriptableObject     (   Resources/Effects/Container). 

              .
 
public class Bullet : MonoBehavior
{
    [SerializeField]
    [IntAttribute(Effects/Container)]
    private int effectId = -1;    
}




( ), , . . , , , .

P.S.: , , , :

CustomEditor;
CustomPropertyDrawer;
EditorWindow;
Debug ;
Gizmos.

. , , .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331042/

:  

: [1] []
 

:
: 

: ( )

:

  URL