using UnityEngine; using UnityEditor.Experimental.AssetImporters; using System.IO; [ScriptedImporter(1, "jjj")] public class Script_03_11 : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); Vector3 position = JsonUtility.FromJson<Vector3>(File.ReadAllText(ctx.assetPath)); cube.transform.position = position; cube.transform.localScale = Vector3.one; Material mat = new Material(Shader.Find("Standard")); mat.color = Color.green; ctx.AddObjectToAsset("material", mat); cube.GetComponent<MeshRenderer>().material = mat; ctx.AddObjectToAsset("obj",cube); ctx.SetMainObject(cube); } }
[ScriptedImporter(1, "jjj")] 表示自定义的资源后缀为jjj,导入该类型的文件时,就会执行OnInportAsset方法,创建一个Cube,位置信息使用自定义资源的json数据,赋值一个材质,材质需要保存起来,不然就会丢失。