Instantiate class and set properties from the editor view Unity C#

PetqImaQkaputka

I have the following class :

[Serializable]
public class BaseDamage : MonoBehaviour
{
    public int MaximumDamage;
    public int MinimumDamage;
    public short SpellScaling;
}

and a class which contains that class :

public class SpellDamage : MonoBehaviour
{
    public BaseDamage SpellBaseDamage;
}

When I attach SpellDamage script to a gameObject I want to be able to set the value of BaseDamage from the editor view, but it just allows me to attach a script to it.

how can i do that ?

Bizhan

You are on the right track. You can attach the BaseDamage script to the same gameobject which SpellDamage script is attached to.

You've added the Serializable attribute. What you need to do is write a Custom Property Drawer for BaseDamage class inside Editor folder.

Something like this:

[CustomPropertyDrawer(typeof(BaseDamage))]
public class BaseDamageDrawer : PropertyDrawer
{


static string[] propNames = new[]
{
    "MaximumDamage",
    "MinimumDamage",
    "SpellScaling",
};



public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
    // Using BeginProperty / EndProperty on the parent property means that
    // prefab override logic works on the entire property.
    EditorGUI.BeginProperty(position, label, property);

    // Draw label
    position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

    // Don't make child fields be indented
    var indent = EditorGUI.indentLevel;
    EditorGUI.indentLevel = 0;

    SerializedProperty[] props = new SerializedProperty[propNames.Length];
    for (int i = 0; i < props.Length; i++)
    {
        props[i] = property.FindPropertyRelative(propNames[i]);
    }


    // Calculate rects
    float x = position.x;
    float y = position.y;
    float w = position.width / props.Length;
    float h = position.height;
    for (int i = 0; i < props.Length; i++)
    {
        var rect = new Rect(x, y, w, h);
        EditorGUI.PropertyField(rect, props[i], GUIContent.none);
        x += position.width / props.Length;
    }

    // Set indent back to what it was
    EditorGUI.indentLevel = indent;

    EditorGUI.EndProperty();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Instantiate class and set properties from the editor view Unity C#

From Dev

When posting data from view all the properties are set to null except the ones having editor field

From Dev

Unity C# – Accessing subclass properties from variable defined as derived class

From Dev

How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

From Dev

How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

From Dev

Instantiate class from within a class

From Dev

C# Unity how do we resolve/instantiate class for multi level class method calls

From Dev

C# Unity how do we resolve/instantiate class for multi level class method calls

From Dev

Jump from Text Editor to Class View in VS2017

From Dev

Instantiate class and also assign properties with one statement

From Dev

Unity 5: access/modify AnimatorController from C# script in editor

From Dev

Instantiate class from protocol type

From Dev

Using A SeT Property To Set Multiple Properties Of A Class Simultaneously In C#

From Dev

How to set QML properties from C++

From Dev

How to set properties on initial view controller (created in Storyboard) from AppDelegate?

From Dev

Prism5 + Unity, set view from another module for PopupWindowAction

From Dev

C++ Modifying properties from a base class

From Dev

Unity C# Instantiate prefab not working correctly

From Dev

In C#, if the (string) class name is known, how to instantiate an object from that (string) class?

From Dev

Set/Access WCF Class level properties from IOS request

From Dev

Instantiate new "players" in a sub folder in Hierarchy view | Unity networking with Vuforia

From Dev

Set properties in the way it's declared in c# class

From Dev

access dynamic properties of a dynamic class and set value in C#

From Dev

Visual Studio dump all properties of class into editor

From Dev

Copy class properties from class to a new subclass C#

From Dev

Instantiate all properties and sub properties in an object (C#.net)

From Dev

Instantiate all properties and sub properties in an object (C#.net)

From Dev

Instantiate Swift View Controller in Objective C

From Dev

How to set initial view properties?

Related Related

  1. 1

    Instantiate class and set properties from the editor view Unity C#

  2. 2

    When posting data from view all the properties are set to null except the ones having editor field

  3. 3

    Unity C# – Accessing subclass properties from variable defined as derived class

  4. 4

    How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

  5. 5

    How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

  6. 6

    Instantiate class from within a class

  7. 7

    C# Unity how do we resolve/instantiate class for multi level class method calls

  8. 8

    C# Unity how do we resolve/instantiate class for multi level class method calls

  9. 9

    Jump from Text Editor to Class View in VS2017

  10. 10

    Instantiate class and also assign properties with one statement

  11. 11

    Unity 5: access/modify AnimatorController from C# script in editor

  12. 12

    Instantiate class from protocol type

  13. 13

    Using A SeT Property To Set Multiple Properties Of A Class Simultaneously In C#

  14. 14

    How to set QML properties from C++

  15. 15

    How to set properties on initial view controller (created in Storyboard) from AppDelegate?

  16. 16

    Prism5 + Unity, set view from another module for PopupWindowAction

  17. 17

    C++ Modifying properties from a base class

  18. 18

    Unity C# Instantiate prefab not working correctly

  19. 19

    In C#, if the (string) class name is known, how to instantiate an object from that (string) class?

  20. 20

    Set/Access WCF Class level properties from IOS request

  21. 21

    Instantiate new "players" in a sub folder in Hierarchy view | Unity networking with Vuforia

  22. 22

    Set properties in the way it's declared in c# class

  23. 23

    access dynamic properties of a dynamic class and set value in C#

  24. 24

    Visual Studio dump all properties of class into editor

  25. 25

    Copy class properties from class to a new subclass C#

  26. 26

    Instantiate all properties and sub properties in an object (C#.net)

  27. 27

    Instantiate all properties and sub properties in an object (C#.net)

  28. 28

    Instantiate Swift View Controller in Objective C

  29. 29

    How to set initial view properties?

HotTag

Archive