Unity特性:使用特性与反射对UI进行prefab绑定
发布日期:2021-05-12 18:11:30 浏览次数:12 分类:精选文章

本文共 4370 字,大约阅读时间需要 14 分钟。

������������������

���������C#���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������C#���������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������
  • ���������������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ���������������UI Prefab������������

    ���������������

    ���������������UI������������������������������������������������������������Prefab���������������������������������UI���������������������������������������������������������������������������Prefab������������������������

    ���������������

  • ���������������

    ������������������������Attribute������������[AttributeUsage(...)] attribute ���������������������������������������������������������

    [AttributeUsage(AttributeTargets.Class)]  public class BindPrefab : Attribute  {      public string Path { get; private set; }      public BindPrefab(string path)      {          Path = path;      }  }

    ���������������������Path������������������������������������������������

  • ������������

    ���������������Prefab������������������������������������������������������

    [BindPrefab("Prefab/StartViewUI")]  public class StartView  {      // ���������������������������������Path������      // ������������������Prefab���������  }
  • ���������������

    ���������������������������������������������������������������������������������������������������������������������Dictionary���������Prefab���������������������

    public class BindAttribute  {      private static readonly Dictionary
    mPrefabAndScriptMap = new Dictionary
    (); public class Binder { public void Process(Assembly assembly) { var types = assembly.GetExportedTypes(); foreach (var type in types) { var attributes = Attribute.GetCustomAttributes(type); foreach (var attr in attributes) { if (attr is BindPrefab) { var path = ((BindPrefab)attr).Path; if (!mPrefabAndScriptMap.ContainsKey(path)) { mPrefabAndScriptMap.Add(path, type); } else { Logger.LogWarning("Duplicate prefab path '" + path + "'"); } } } } } } }
  • ������������������������������������������������������������������������������������������������������������������������WWML���UI���������������������������������������������������������

    ������������������������

    ������������������������������������������������������������������������������������������������

    • ������������������

      ������������������������������������������������������������������������������������������������������������������������������������������������

    • ���������

      ���������������������������������������������������������������������������������������������������

    • ������������������

      ���������������������������������������������������������������������������������������������������������

    ���������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:Lua-table 一种更少访问的安全取值方式
    下一篇:游戏设计模式与实现之:工厂模式 与 资源工厂

    发表评论

    最新留言

    哈哈,博客排版真的漂亮呢~
    [***.90.31.176]2025年04月05日 01时47分16秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章