调用枚举类型

CallCSEnum.lua.txt

  1. local callEnum = CS.UnityEngine.Object.FindObjectOfType(typeof(CS.shenjun.CallEnum))
  2. callEnum:EnemyAttack(callEnum.myType)
  3. --直接访问枚举
  4. local boss = CS.shenjun.EnemyType.Boss
  5. print("===========", boss) -- Boss : 1
  6. callEnum:EnemyAttack(2)
  7. --把字符串转换成枚举
  8. local npc = CS.shenjun.EnemyType.__CastFrom('NPC')
  9. print("===========", npc)
  10. callEnum:EnemyAttack(npc)
  11. --把数字转换成枚举
  12. local mon = CS.shenjun.EnemyType.__CastFrom(0)
  13. print("===========", mon)
  14. callEnum:EnemyAttack(mon)

CallCSEnum.cs

  1. /*
  2. * created by shenjun
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using XLua;
  8. namespace shenjun
  9. {
  10. public class CallEnum : MonoBehaviour {
  11. public EnemyType myType = EnemyType.Boss;
  12. void Start () {
  13. LuaEnv luaEnv = new LuaEnv();
  14. luaEnv.DoString("require 'CallCSEnum'");
  15. luaEnv.Dispose();
  16. }
  17. void Update () {
  18. }
  19. public void EnemyAttack(EnemyType type)
  20. {
  21. Debug.Log("EnemyType : " + type);
  22. }
  23. }
  24. [LuaCallCSharp]
  25. public enum EnemyType
  26. {
  27. Monster,
  28. Boss,
  29. NPC,
  30. }
  31. }

?