欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > Unity实现自定义图集(一)

Unity实现自定义图集(一)

2024/10/26 15:14:08 来源:https://blog.csdn.net/weixin_44165354/article/details/142781068  浏览:    关键词:Unity实现自定义图集(一)

以下内容是根据Unity 2020.1.0f1版本进行编写的

Unity自带有图集工具,包括旧版的图集(设置PackingTag),以及新版的图集(生成SpriteAtlas)。一般来说,unity自带的图集系统已经够用了,但是实际使用上还是存在一些可优化的地方,例如加载到Canvas上的资源,打图集不能同时设置TightPacking和AllowRotation,即打图集不能旋转。

此外,也可以更加深入地了解图集的运行逻辑,以及ui材质球的合批。

1、先看看两种打图集方法的效果

关于unity是怎么打图集的,这里有一个链接可以参考:
SpriteAtlas图集预览算法:MaxRectsBinPack
本文是基于这个基础上进行改动的。
按照上述博文的内容,先简单尝试一下,实现其中的内容,效果:
图片资源(资源为网络资源):

Unity的SpriteAtlas打出来的图集:
在这里插入图片描述
根据博文得到的图集:
在这里插入图片描述
(代码中有5种不同的匹配方式,实际结果都大同小异)
可以看到,unity打出来的图集会比上述博文中打出来的图集更小。细看一下发现,是Unity会自动把一些资源周围的空白像素裁掉。

2、旋转资源

先简单做一个ui预制和一个场景预制,都是用相同的资源做的(上面是ui,下面是SpriteRenderer)
在这里插入图片描述
然后把图集改成可旋转
在这里插入图片描述
可以看到部分资源旋转了
接下来运行unity
在这里插入图片描述
可以看到,上半部分ui的界面部分资源方向不对,甚至还把其它资源重叠显示了。

3、接入一个简单的资源加载系统

此处不是重点,简单过一遍,跳过。

using UnityEngine;
/// <summary>
/// 单例类父类
/// </summary>
/// <typeparam name="T"></typeparam>
public class Singleton<T> where T:new()
{private static readonly object _lock = new object();private static T _instance;protected Singleton(){Debug.Assert(_instance == null);}public static bool Exists{get{return _instance != null;}}public static T Instance{get{if (_instance == null){lock (_lock){if (_instance == null){_instance = new T();}}}return _instance;}}
}

实现单例类Singleton

using UnityEngine;public class GameController : MonoBehaviour
{void Start(){
#if UNITY_EDITORAssetsCache.Instance.Init(ResourceLoadMode.Direct);
#elseAssetsCache.Instance.Init(ResourceLoadMode.AssetBundle);
#endif}
}

新建一个游戏管理类GameController,用于初始化资源加载器。
然后在场景上新建一个空节点,命名为Scripts,将GameController挂载到该节点上。
此时可以把canvas下的预制删掉了,如下图:
在这里插入图片描述

using UnityEngine;public class TestView : MonoBehaviour
{const string viewPath = "Assets/Prefabs/testview_myimage.prefab";GameObject viewPrefab;GameObject canvas;void Start(){canvas = GameObject.Find("Canvas");AssetsCache.Instance.GetResource(viewPath, OnGetView);}void OnGetView(AssetsResource asset){viewPrefab = Instantiate(asset.GetAsset(viewPath), canvas.transform) as GameObject;}
}

新建一个类TestView,用于加载ui预制到Canvas节点下,并将其挂载到刚刚创建的Scripts节点上:
在这里插入图片描述
在这里插入图片描述
保存场景,运行unity,可以看到预制正常加载出来了。

4、实现一个简单的打包逻辑

此处也不是重点,简单过一遍。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;public class AppConst
{// 素材目录 public const string AssetDir = "StreamingAssets";//打包时需要每个子文件夹打一个bundle的目录private static List<string> subDirectoryBundlePaths;//打包时需要每个文件打一个bundle的目录private static List<string> eachFileBundlePaths;//打包时需要将整个文件夹打一个bundle的目录private static Dictionary<string, string> wholeDirectoryBundlePaths;private static bool hasInit = false;public static void Init(){subDirectoryBundlePaths = new List<string>();eachFileBundlePaths = new List<string>();wholeDirectoryBundlePaths = new Dictionary<string, string>();wholeDirectoryBundlePaths.Add("prefab.bundle", "Prefabs/");hasInit = true;}public static List<string> GetSubDirectoryBundlePaths(){if(!hasInit){Init();}return subDirectoryBundlePaths;}public static List<string> GetEachFileBundlePaths(){if (!hasInit){Init();}return eachFileBundlePaths;}public static Dictionary<string, string> GetWholeDirectoryBundlePaths(){if (!hasInit){Init();}return wholeDirectoryBundlePaths;}public static Hashtable GetResourcesHashTable(){Hashtable tb = new Hashtable();foreach(var path in GetSubDirectoryBundlePaths()){DirectoryInfo[] directories = new DirectoryInfo(Application.dataPath + path).GetDirectories();foreach (var directory in directories){string assetsPath = UnifyPath(directory.FullName).Substring(7);tb[directory.Parent.Name + "." + directory.Name + ".bundle"] = assetsPath;}}foreach (var path in GetEachFileBundlePaths()){List<FileSystemInfo> files = new List<FileSystemInfo>();SeachFile(Application.dataPath + path, files, new string[] { ".png", ".jpg", ".jpeg", ".tga" });foreach (var file in files){string assetsPath 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com