Unity自定义资源导入器(unity实验性功能)
作者:互联网
官方链接:
https://docs.unity.cn/cn/2019.4/Manual/ScriptedImporters.html
https://docs.unity3d.com/cn/2018.4/Manual/ScriptedImporters.html(Scripted Importer)
https://docs.unity.cn/cn/2019.4/ScriptReference/Experimental.AssetImporters.AssetImportContext.html(AssetImportContext)
这是这个案例用到的两个api的链接,案例取自第一个链接,注释了一句代码,那句代码查了api没明白没用的,而且会导致报错。
using UnityEngine; using UnityEditor.Experimental.AssetImporters; using System.IO; [ScriptedImporter(1, "cube")] public class Test4 : ScriptedImporter { public float m_Scale = 1; public override void OnImportAsset(AssetImportContext ctx) { var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); GameObject.Instantiate(cube); //var position = JsonUtility.FromJson<Vector3>(File.ReadAllText(ctx.assetPath)); //cube.transform.position = position; cube.transform.localScale = new Vector3(m_Scale, m_Scale, m_Scale); // 'cube' is a a GameObject and will be automatically converted into a Prefab // (Only the 'Main Asset' is elligible to become a Prefab.) ctx.AddObjectToAsset("main obj", cube); ctx.SetMainObject(cube); var material = new Material(Shader.Find("Standard")); material.color = Color.red; // Assets must be assigned a unique identifier string consistent across imports ctx.AddObjectToAsset("my Material", material); // Assets that are not passed into the context as import outputs must be destroyed var tempMesh = new Mesh(); DestroyImmediate(tempMesh); } }
这个案例的用处是可以自定义一个后缀文件导入器,案例中像特性一样使用的其实是继承方法的构造。当你导入一个后缀为“cube"的时候,就会执行这个方法,在unity中会产生一个模型文件(同fbx),会有一个材质球为子物体。我尝试过把”prefab“,"fbx",加入到这个构造,会报错,不能自定义。
转自个人简书:2020.10.19完成
标签:cube,cn,自定义,ctx,Unity,unity,Scale,var 来源: https://www.cnblogs.com/zjr0/p/16407702.html