AssetDatabase.LoadMainAssetAtPath

Switch to Manual
public static Object LoadMainAssetAtPath (string assetPath);

Parameters

assetPath@param assetPath Путь файловой системы ассета для загрузки.

Description

Возвращает объект главного ассета в assetPath.

Все пути относятся к папке проекта, например: "Assets/MyTextures/hello.png".

See Also: AssetDatabase.LoadAssetAtPath, AssetDatabase.LoadAllAssetsAtPath, AssetDatabase.LoadAllAssetRepresentationsAtPath.

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class MyPlayer : MonoBehaviour { [MenuItem("AssetDatabase/AssignMaterialsToModels")] static void AssignGunMaterialsToModels() { //Get all the materials that have the name gun in them using LoadMainAssetAtPath var materials = new List<Material>(); 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; } } }