IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity 预制体转换工具 -> 正文阅读

[游戏开发]Unity 预制体转换工具

在Unity开发过程中,会创建很多prefab或者使用一些素材包,由于更换Unity版本或者版本与素材包导出版本不一致,会导致prefab损坏的情况,针对这些情况,我制作了这个预制体转换工具,用于将高版本的预制体转换为低版本Unity可使用的格式。
在这里插入图片描述

输入由于版本不一致导致损坏的预制体所在文件夹路径(Assets开头),并点击开始转换。

EditorGUI窗口绘制:

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

public class PrefabConvertWindow : EditorWindow
{
    public static string PrefabDirPath = "Assets/Prefabs";

    [MenuItem("Wonderland6627/PrefabConvertTools/PrefabConvertWindow", priority = 0)]
    public static void OpenWindow()
    {
        EditorWindow.GetWindow<PrefabConvertWindow>();

        PrefabDirPath = PlayerPrefs.GetString("PrefabDirPathSaveKey");
    }

    private void OnGUI()
    {
        GUILayout.Space(10);
        EditorGUILayout.LabelField("预制体文件夹路径(Assets开头)", EditorStyles.boldLabel);
        PrefabDirPath = EditorGUILayout.TextField(PrefabDirPath);

        GUILayout.Space(10);
        if (GUILayout.Button("开始转换"))
        {
            if (string.IsNullOrEmpty(PrefabDirPath) || !PrefabDirPath.StartsWith("Assets") || !Directory.Exists(PrefabDirPath))
            {
                Debug.LogError("路径有误");
                
                return;
            }

            PrefabConvertTools.FindAllPrefabInPath(PrefabDirPath);
        }

        GUILayout.Space(10);
        if (GUILayout.Button("关闭进度条"))
        {
            EditorUtility.ClearProgressBar();
        }
    }

    private void OnLostFocus()
    {
        PlayerPrefs.SetString("PrefabDirPathSaveKey", PrefabDirPath);
    }

    private void OnDisable()
    {
        PlayerPrefs.SetString("PrefabDirPathSaveKey", PrefabDirPath);
    }
}

转换方法:

public class PrefabConvertTools
{
    public static void FindAllPrefabInPath(string dirPath)
    {
        PlayerPrefs.SetString("PrefabDirPathSaveKey", dirPath);
        string[] allPath = AssetDatabase.FindAssets("t:Prefab", new string[] { dirPath });
        int length = allPath.Length;

        EditorUtility.DisplayProgressBar("处理预制体", "", 0);
        for (int i = 0; i < allPath.Length; i++)
        {
            string path = AssetDatabase.GUIDToAssetPath(allPath[i]);
            if (path.Contains("_Converted"))
            {
                continue;
            }

            var obj = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
            if (obj != null)
            {
                EditorUtility.DisplayProgressBar("处理预制体", obj.name, (float)i / length);
                ConvertPrefab2LowVersion(obj);
            }
        }

        EditorUtility.ClearProgressBar();
        Debug.Log("处理完成");
    }

    /// <summary>
    /// Prefab高版本转换低版本
    /// 带上_Converted后缀来代表转换好的obj
    /// </summary>
    public static void ConvertPrefab2LowVersion(GameObject oldPrefab)
    {
        string rootDirPath = GetObjRootDirPath(oldPrefab, true);
        string newName = oldPrefab.name + "_Converted.prefab";
        PrefabUtility.CreatePrefab(rootDirPath + newName, oldPrefab);
    }

    /// <summary>
    /// 获取文件父文件夹的Assets目录 isSuffix是否带有'/'
    /// </summary>
    private static string GetObjRootDirPath(UnityEngine.Object obj, bool isSuffix = false)
    {
        string fullPath = AssetDatabase.GetAssetPath(obj);
        string dirPath = string.Empty;
        var dirs = fullPath.Split('/');
        for (int i = 0; i < dirs.Length - 1; i++)
        {
            dirPath += dirs[i] + "/";
        }

        return isSuffix ? dirPath : dirPath.Remove(dirPath.Length - 1);
    }
}

转换完成之后,会在指定文件夹内生成“原文件名_Converted.prefab"的预制体,即为转换完成的新预制体。
原理大概是Unity的预制体是一种可序列化文件,其格式在不同的Unity版本有微小的不同,而AssetDatabase.LoadAssetAtPath方法能够将文件正常读取,再通过PrefabUtility.CreatePrefab去创建符合当前版本序列化规则的预制体,就能够正常加载不会报prefab broken的警告了。

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-16 12:04:02  更:2021-08-16 12:04:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 19:08:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码