• unity3d 依赖关系获取预制件任意资源


    前段时间策划们想知道UI预制件中使用了哪些音效

    N多预制件、N多音效!!

    如果纯人工整理的话这还不累成狗?

    累成狗不说,还容易出错

    所以获取音频剪辑小工具就诞生了,将策划从死亡边缘拉了回来

    我们先看一下相关API手册:http://game.ceeger.com/Script/EditorUtility/EditorUtility.CollectDependencies.html

    这玩意可好用了,之前也用它做过获取预制件公共资源工具

    看看名字:“收集依赖关系”

    那还不简单明了了,有依赖关系的东西都能获取过来嘛

    我写了个方法,支持获取任意类型

    这个方法你可以直接copy去用,不用自己写的。

    使用方法也很简单,在这里我提供一个完整的获取预制件音频剪辑的例子。上代码咯

     1 #region HeadComments
     2 /* ========================================================================
     3 * Copyright (C) 2015 Arthun
     4 *
     5 * 作    者:Arthun
     6 * 文件名称:ArthunTools
     7 * 功    能:升哥哥工具包
     8 * 创建时间:2015/09/11 2:15:20
     9 * 版    本:v1.0.0
    10 *
    11 * [修改日志]
    12 * 修改者: 时间: 修改内容:
    13 * 
    14 * =========================================================================
    15 */
    16 #endregion
    17 
    18 using System.Collections.Generic;
    19 using UnityEditor;
    20 using UnityEngine;
    21 
    22 public class ArthunTools : EditorWindow
    23 {
    24     /// 音频剪辑信息 <summary>
    25     /// 
    26     /// </summary>
    27     public class AudioClipInfo
    28     {
    29         public string prefab = "";
    30         public string audio = "";
    31     }
    32 
    33     /// 选中预制件(可多选)后按 Alt + G 键获取 <summary>
    34     /// 
    35     /// </summary>
    36     [MenuItem("Arthun Tools/Get Select Prefabs Info &G")]
    37     static void GetSelectPrefab()
    38     {
    39         GameObject[] gos = Selection.gameObjects;
    40 
    41         Debug.Log("getCount:" + gos.Length.ToString());
    42 
    43         if (gos.Length == 0)
    44             return;
    45 
    46         List<AudioClipInfo> audioClipsInfo = new List<AudioClipInfo>();
    47         foreach (GameObject go in gos)
    48         {
    49             #region 依赖关系获取音频
    50             List<AudioClip> audioClips = GetPrefabDepe<AudioClip>(go);
    51 
    52             foreach (AudioClip ac in audioClips)
    53             {
    54                 if (ac != null)
    55                 {
    56                     AudioClipInfo info = new AudioClipInfo();
    57                     info.prefab = go.name;
    58                     info.audio = ac.name;
    59                     audioClipsInfo.Add(info);
    60                 }
    61             }
    62             #endregion
    63         }
    64 
    65         foreach (AudioClipInfo info in audioClipsInfo)
    66         {
    67             Debug.Log(string.Format("prefab:{0}    audio:{1}", info.prefab, info.audio));
    68         }
    69 
    70         Debug.Log("soundCount:" + audioClipsInfo.Count.ToString());
    71     }
    72 
    73     /// 获取预制件依赖 <summary>
    74     /// 
    75     /// </summary>
    76     /// <typeparam name="T">欲获取的类型</typeparam>
    77     /// <param name="go"></param>
    78     /// <returns></returns>
    79     static List<T> GetPrefabDepe<T>(GameObject go)
    80     {
    81         List<T> results = new List<T>();
    82         Object[] roots = new Object[] { go };
    83         Object[] dependObjs = EditorUtility.CollectDependencies(roots);
    84         foreach (Object dependObj in dependObjs)
    85         {
    86             if (dependObj != null && dependObj.GetType() == typeof(T))
    87             {
    88                 results.Add((T)System.Convert.ChangeType(dependObj, typeof(T)));
    89             }
    90         }
    91 
    92         return results;
    93     }
    94 }

    文中不足之处欢迎批评指正

    本文链接:http://www.cnblogs.com/shenggege/p/4799801.html

  • 相关阅读:
    Iterator 迭代器
    Collection-List
    Collection-Set
    Collection
    多线程
    面向对象<高级>知识点
    链表
    面向对象<基础>知识点
    三层架构和MVC模式详解
    impala为什么比hive快
  • 原文地址:https://www.cnblogs.com/shenggege/p/4799801.html
Copyright © 2020-2023  润新知