场景中材质球和贴图打印

news/2024/7/4 13:23:09

在项目中,场景大小很重要,特别是场景多了以后,除了在场景上做动态的加载,还要及时卸载。

为了可以在更多的安卓手机上可以运行,就需要对于场景加以优化。

1.shader上,尽量使用程序员找的shader,不要找一些效果很好,但是在一些手机上蹦的shader.

在现在的项目中,有一个描边发光的shader,在三星的手机上就很容易蹦,根本是这个shader需要描边的渲染,而三星的手机上显卡不满足就蹦了。使用程序给予的shader的好处是,在技术支持最大的范围的内,做出做好的效果。

2.场景的贴图上,不要使用过多的贴图,贴图过大,就会占用很大的内存。一张贴图,1024*1024,格式ARBG的,有5.7M,加载到就2倍,低于内存本身就小的机型,2个贴图就不让干别的了。目前项目中要求,2张2048*2048就极限了。在格式上安卓选用ETC1,iphone上使用PVR。就达到了低于图片的优化

3.在sence做完了,执行这个代码就可以获得相应的材质球和贴图信息了。

FindTextureName.cs

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

public class Material_Texture_Calculation:MonoBehaviour 
{
	public class AllInfo
	{
		private string materialName;
		private List<string> names;
		private List<float> sizes;
		float maxsize;
		public AllInfo()
		{
			maxsize = 0;
			materialName = "";
			names = new List<string>();
			sizes = new List<float>();
		}
	


		public string MaterialName
		{
			get
			{
				return materialName;
			}
			set
			{
				value = materialName;
			}
		}
		public int Length
		{
			get
			{
				return names.Count;
			}
		}

		public float MaxSize
		{
			get
			{
				return maxsize;
			}
		}
		public List<string> TextureNames
		{
			get
			{
				return names;
			}
		}

		public List<float> TextureSizes
		{
			get
			{
				return sizes;
			}
		}

		public void Add(string TextureNames,float size)
		{
			names.Add (TextureNames);
			sizes.Add (size);
		}
		public void Add(string materialname)
		{
			materialName = materialname;
		}

		public void sort()
		{
			if (Length > 0)
			{
				for(int i=0;i<Length;i++)
				{
					for(int j=0;j<Length;j++)
					{
						float tempint;
						string tempstr;
						tempint = sizes[j];
						sizes[j] = sizes[i];
						sizes[i] = tempint;
						tempstr = names[j];
						names[j] = names[i];
						names[i] = tempstr;
					}
				}
				maxsize = sizes[0];
			}
		}

		public void Print()
		{
			UnityEngine.Debug.Log ("Material Name:" + MaterialName);
			for (int i=0; i<Length; i++)
			{
    				UnityEngine.Debug.Log(" |Texture Name:" names[i]+ "   Texture Size:" +sizes[i] + "kb|");
			}
		}
	}

	[MenuItem("Tools/Calualation")]
	public static void Calualation()
	{
		GameObject[] gameObjects = GameObject.FindObjectsOfType<GameObject> ();
		List<string> materialnames = new List<string> ();
		List<string> pngnames = new List<string> ();
		List<AllInfo> calualation = new List<AllInfo> ();

		for (int i=0; i<gameObjects.Length; i++) 
		{
			Renderer render = gameObjects[i].GetComponent<Renderer>();
			if(null!=render)
			{
				Material material = render.sharedMaterial;
				Material[] materials = render.sharedMaterials;
				int count = materials.Length;
				for(int j=0;j<count;j++)
				{
					if(null !=materials[j])
					{
						if(materialnames.Contains(materials[j].name));
						continue;
					}
					materialnames.Add(materials[j].name);
					AllInfo allinfo = new AllInfo();
					allinfo.Add (materials[j].name);
					Shader shader = materials[j].shader;
					if(null != shader)
					{
						int propertyNum = ShaderUtil.GetPropertyCount(shader);
						for(int k=0;k<propertyNum;k++)
						{
							if(ShaderUtil.GetPropertyType(shader,k)==ShaderUtil.ShaderPropertyType.TexEnv)
							{
								string textureName = ShaderUtil.GetPropertyName(shader,k);
								Texture texture = materials[j].GetTexture(textureName); 
								if(null != texture)
								{
									float texturesize = Profiler.GetRuntimeMemorySize(texture)/2048.0f;
									string textureRealName = texture.name;
									if(!pngnames.Contains(textureRealName))
									{
										pngnames.Add(textureRealName);
										allinfo.Add(textureRealName,texturesize);
									}
								}
							}
						}
					}
					allinfo.sort();
					calualation.Add(allinfo);
				}
			}
		}
		AddByDescending(calualation);
		print (calualation);
		PrintToFile(calualation);
		UnityEngine.Debug.LogWarnging("Material Number:" + materialnames.Count);
		UnityEditor.Debug.LogWarnging("Textures Name:" + pngnames.Count);
		PrintResults(materialnames,pngnames);
	}
	public static void AddByDescending(List<AllInfo> list)
	{
		int count = list.Count;
		for (int i=0; i<count-1; i++) 
		{
			for(int j=i+1;j<count;j++)
			{
				AllInfo allInfo;
				if(list[i].MaxSize<list[j].MaxSize)
				{
					allInfo = list[i];
					list[i] = list[j];
					list[j] = allInfo; 
				}
			}
		}
	}

	public static void Print(List<AllInfo> list)
	{
		for (int i=0; i<list.Count; i++) 
		{
			list[i].Print();
		}
	}
	 public static void PrintToFile(List<AllInfo> list)
	{
		string path = Application.dataPath + "/Calculation.txt";
		if (File.Exists (path)) 
		{
			File.Delete(path);
		}
		FileStream filestream = File.Create (path);
		StringWriter steamwriter = new StreamWriter(filestream);
		int count = list.Count;
		for (int i=0; i<count; i++) 
		{
			int length = list[i].Length;
			string zhushi = "++++++++++++";
			steamwriter.WriteLine(zhushi);
			string material = "->|Material Name:" + list[i].MaterialName+"|<-";
			steamwriter.WriteLine(material);
			for(int j=0;j< length;j++)
			{
				string name = "     |Texture Name:" + list[i].TextureNames[j]+"  Texture Szie:" + list[i].TextureSizes[j]+"KB|";
				steamwriter.WriteLine(name);
			}
		}
		steamwriter.Flush ();
		steamwriter.Close ();
		filestream.Close ();
	}

	public static void PrintResultes(List<string> mlist,List<string> plist)
	{
		string path = Application.dataPath + "/Calculation.txt";
		if (File.Exists (path)) 
		{
			StreamWriter streamwriter = new StreamWriter(path,true);
			string zhushi1 ="******************";
			string str1 = "Material Number: " + mlist.Count;
			string zhushi2 = "****************";
			string str2 = "Texture Number:" + plist.Count;
			string zhushi3 = "*****************";
			streamwriter.WriteLine(zhushi1);
			streamwriter.WriteLine(str1);
			streamwriter.WriteLine(zhushi2);
			streamwriter.WriteLine(str2);
			streamwriter.WriteLine(zhushi3);
			streamwriter.Flush();
			streamwriter.Close();
			Process.Start(path);
		}
	}
}
首先要对unity的api了解,获取材质球Render,其次对于有多通道贴图的shader,unity 给予了获取shader属性的方法,对于贴图大小,
Profiler.GetRuntimeMemorySize(texture)/2048.0f;
在其他把重复使用到的材质球和贴图名称剔除。

在Editor文件夹下,同时生成在一个txt文件中,而是不使用unity的debug,方便做历史记录的对比。


http://www.niftyadmin.cn/n/4204516.html

相关文章

java函数前加个条件_java里为什么主函数前面要加static修饰 | 学步园

先说一下static1.static 修饰的域&#xff0c;我们叫静态域&#xff0c;它是归类所有的&#xff0c;被所有对象所共享&#xff0c;只有一个2.static修饰的区块&#xff0c;域只会初始化一次3.static修饰的域或方法&#xff0c;可以直接通过类的名字加上.进行调用4.static修饰的…

【雕爷学编程】Arduino动手做(138)---64位WS2812点阵屏模块3

37款传感器与执行器的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&am…

js 数组插入删除

常用的方法是遍历数组&#xff0c;然后使用splice&#xff08;&#xff09;删除 这里我们使用es6 中findIndex&#xff08;&#xff09;查找&#xff0c;然后删除 function deleteFromArray(arr, compare) {const index arr.findIndex(compare)if (index > -1) {arr.spli…

在vue项目中 如何定义全局变量 全局函数

这里写自定义目录标题设置一个专用的的全局变量模块文件&#xff0c;模块里面定义一些变量初始状态&#xff0c;用export default 暴露出去&#xff0c;需要时导入即可一、变量二、方法设置一个专用的的全局变量模块文件&#xff0c;模块里面定义一些变量初始状态&#xff0c;用…

Unity定时器

需求是在项目中&#xff0c;会遇到很多倒计时功能&#xff0c;比如在线时间奖励&#xff0c;pve活动间隔5分钟 总结常见解决方法有三种 1.Update()每帧检查&#xff0c;适合用于界面上显示具体的数值&#xff0c;这是了准确性。在用户体验倒计时是最好的选择。 2.this.Invoke( …

编写高性能 Web 应用程序的10个技巧

常见的 ASP.NET 性能神话 有用的 ASP.NET 性能技巧和诀窍 在 ASP.NET 中处理数据库的一些建议 缓冲以及用 ASP.NET 进行后台处理 本文使用下列技术&#xff1a;ASP.NET&#xff0c;.NET 框架&#xff0c;IIS   用 ASP.NET 编写 Web 应用程序其轻松程度令人难以置信。它是…

java语言编程的风格_什么是良好的编程风格(Java编程)

展开全部Java编程风格与命名规范整理基本命名规范1.包命名包名按照域名的范围从e69da5e887aa62616964757a686964616f31333431353963大到小逐步列出&#xff0c;恰好和Internet上的域名命名规则相反。由一组以“。”连接的标识符构成&#xff0c;通常第一个标识符为符合网络域名…

vue.js中实现在弹框外有鼠标点击事件时隐藏弹框

mounted() {// 监听页面的点击事件&#xff0c;如果鼠标在pop弹出框和按钮外点击&#xff0c;那么让弹出框不显示document.onclick () > {let e e || window.event;let elem e.srcElement || e.target;while (elem) {if (elem.id "headerSearch" || elem.id …