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完成
posted @   过往云烟吧  阅读(322)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示