V
V
Vladimir Lobanov2022-04-08 01:57:03
Unity
Vladimir Lobanov, 2022-04-08 01:57:03

How to add constants to public enum from another script?

The bottom line is this: there is a compiled dll (Game.dll, there is ALL the game logic, there is no way to rebuild) containing a list of AmmoType.cs constants used both in the game and in the Inspector (custom editor, drop-down list in the Inspector). Task: add constants so that they are visible and selectable in the Inspector. How to implement it? There are no problems for the new constants to work in the game, but they are not visible in the Inspector and the asset has to be edited with a notepad.
List: 624ff472d7354789628724.png
Original AmmoType.cs:

using System;

public enum AmmoType
{
  AmmoNone,
  Ammo10mm,
  Ammo5_45mm,
  Ammo9mm,
  Ammo12g,
  Ammo7_62mm,
  Ammo307cal,
  Ammo14_5mm,
  Ammo7mm_54R,
  AmmoBB,
  AmmoBolt,
  Ammo356cal,
  AmmoPB,
  AmmoFuel,
  Ammo6_49mm,
  AmmoRubber
}

My AmmoType.cs with missing constants:
using System;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

//Эксперимент: Добавил 2 типа патронов, работают, но не видны в Inspector
    public enum AmmoType
    {
        Ammo45g = 16,
        Ammo5_7mm = 17
    }

The drawing of this list happens like this (as far as I understand):
using System;
using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
public class EnumFlagsAttributeDrawer : PropertyDrawer
{

  public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  {
    bool flag = !property.hasMultipleDifferentValues;
    if (flag)
    {
      property.intValue = EditorGUI.MaskField(position, label, property.intValue, property.enumNames);
    }
    else
    {
      EditorGUI.LabelField(position, label);
    }
  }
}

If I create another enum with new constants, how can I tell the editor, when the value in AmmoType.cs > 16, takes the values ​​from my enum, for example UserAmmoType.cs, but adds them to the same list? The logic of the game is such that the weapon fires any ammo based on the Type value in the enum that is passed to the asset, and the names are also visible in the editor, again from AmmoType.cs.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nekit Medvedev, 2022-04-08
@NIKROTOS

https://habr.com/ru/post/331042/
You need the item "Execution in the editor". Just ask the script for a list of constants, or come up with something else.

G
GFX Data, 2022-04-08
@ShockWave2048

Can.

public class Lang : MonoBehaviour
{
     public enum LANGS { RU, EN, ES, DE };
}

public class Actor : MonoBehaviour
{
     public Lang.LANGS lang;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question