assetPath | 要加载的资源的文件系统路径。 |
返回位于 assetPath
的主资源对象。
“主”资源是层次结构根部的资源(例如,一个 Maya 文件,其中可能包含多个网格和游戏对象)。
所有路径相对于项目文件夹,例如:“Assets/MyTextures/hello.png”。
其他资源:AssetDatabase.LoadAssetAtPath、AssetDatabase.LoadAllAssetsAtPath、AssetDatabase.LoadAllAssetRepresentationsAtPath。
using System.Collections.Generic; using UnityEngine; using UnityEditor;
public class MyPlayer : MonoBehaviour { [MenuItem("AssetDatabase/Assign Materials To Models")] static void AssignGunMaterialsToModels() { var materials = new List<Material>(); //Get all the materials that have the name gun in them using LoadMainAssetAtPath foreach (var asset in AssetDatabase.FindAssets("t:Material gun")) { var path = AssetDatabase.GUIDToAssetPath(asset); materials.Add((Material)AssetDatabase.LoadMainAssetAtPath(path)); }
var materialID = 0; //Assign gun materials to their corresponding models MeshRenderer foreach (var asset in AssetDatabase.FindAssets("t:Model Gun")) { if (materialID >= materials.Count) materialID = 0; var path = AssetDatabase.GUIDToAssetPath(asset); var material = materials[materialID++]; material.shader = Shader.Find("Standard"); var modelMesh = (MeshRenderer) AssetDatabase.LoadAssetAtPath(path, typeof(MeshRenderer)); modelMesh.sharedMaterial = material; } } }