【发布时间】:2015-10-19 04:55:11
【问题描述】:
我在 void OnGUI() 上调用 Texture2D 函数。我在 5 秒内添加了 5 个预制件,这些预制件在 20 秒内被破坏。这都是使用 Texture2D 的预制件。我不会在使用后破坏纹理。当我运行这个游戏时,它使用了大量的内存并且它正在增加。我需要销毁这个生成的纹理吗?
public static Texture2D Colors(int r,int g, int b)
{
Texture2D texture = new Texture2D(2, 2);
for (int y = 0; y < texture.height; ++y)
{
for (int x = 0; x < texture.width; ++x)
{
Color color = new Color(r, g, b);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
return texture;
}
编辑
好的。这就是我实际上尝试做的。我有一个名为 HealthSystem 的课程 On Github
using UnityEngine;
using System.Collections;
public class HealthSystem : MonoBehaviour {
float healthBarLenght=20f;//Length for Healthbar;
public static void Set_HealthBar(Transform transform,int HealthBarHeight, float CurrentHealth,float MaxHealth,Texture2D BackBar, Texture2D FrontBar)
{
Vector3 screenPosition;
GUIStyle style1 = new GUIStyle();
GUIStyle style2 = new GUIStyle();
float HPDrop = (CurrentHealth / MaxHealth) * healthBarLenght;
screenPosition = Camera.main.WorldToScreenPoint(transform.position);
screenPosition.y = Screen.height - screenPosition.y;
style1.normal.background = BackBar;
GUI.Box(new Rect(screenPosition.x-(healthBarLenght/2),screenPosition.y-20, healthBarLenght,HealthBarHeight),"",style1);
style2.normal.background = FrontBar;
GUI.Box(new Rect(screenPosition.x-(healthBarLenght/2),screenPosition.y-20, HPDrop,HealthBarHeight),"",style2);
}
public static Texture2D Colors(int r,int g, int b)
{
Texture2D texture = new Texture2D(2, 2);
for (int y = 0; y < texture.height; ++y)
{
for (int x = 0; x < texture.width; ++x)
{
Color color = new Color(r, g, b);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
return texture;
}
}
我从另一个脚本调用它
void Start () {
colorback = HealthSystem.Colors (0, 0, 0);
colorfront = HealthSystem.Colors (0, 255, 0);
}
void OnGUI(){
HealthSystem.Set_HealthBar (transform, 2f, 70f, 100f, colorback, colorfront);
}
这个脚本附在我的敌人部队上。 5 秒内出现 5 个敌人。他们将在20秒内被摧毁。这个敌人对象正在使用 Physics.OverlapSphere 来识别他们的敌人
【问题讨论】:
-
"我需要删除吗?"是的,确实可以试试。
-
我是 Unity 新手。那么如何在这个函数中销毁Texture呢。
-
嗯,让我想想这个。如果您知道 Texture2D 纹理始终为 2x 2y ,为什么还要使用 for 循环?您还可以显示 onGUI() 代码吗? :)
-
Texture2D BackBar=Colors(0,255,0); style1.normal.background = BackBar; /*刚才我在这里添加了这段代码*/ Destroy (BackBar);
-
产卵地点在哪里?
标签: memory-management unity3d texture2d