关于Unity Texture2D在Image上的显示问题

macro527 2017-04-12 10:06:14
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ImageTest : MonoBehaviour {

public Image image;
void Start () {


}
void Update () {

}
/// <summary>
/// 点击按钮
/// </summary>
public void Onclick()
{
Texture2D texture = new Texture2D(128, 128);

image.GetComponent<Image>().material.mainTexture = texture;



for (int y = 0; y < texture.height; y++)
{

for (int x = 0; x < texture.width; x++)
{

Color color = ((x & y) != 0 ? Color.white : Color.gray);

texture.SetPixel(x, y, color);

}

}

texture.Apply();
}
}
点击按钮后不显示


using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ImageTest : MonoBehaviour {

public Image image;
void Start()
{
Texture2D texture = new Texture2D(128, 128);

image.GetComponent<Image>().material.mainTexture = texture;



for (int y = 0; y < texture.height; y++)
{

for (int x = 0; x < texture.width; x++)
{

Color color = ((x & y) != 0 ? Color.white : Color.gray);

texture.SetPixel(x, y, color);

}

}

texture.Apply();
}

}
在start中调用则显示
...全文
4677 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoveMollyWendy 2017-04-27
  • 打赏
  • 举报
回复
楼主结贴吧,其实还有其他的解决方式,你更换下材质也能够解决的
LoveMollyWendy 2017-04-27
  • 打赏
  • 举报
回复 1
楼上你们的方向错了,其实不是什么释放不释放的问题,这是unity的锅。 把这一句代码image.GetComponent<Image>().material.mainTexture = texture;换成 image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),new Vector2(0.5f,0.5f)); 就好了
地狱为王 2017-04-14
  • 打赏
  • 举报
回复
就是销毁了。 函数中声明的变量(值类型),函数执行完,就会被释放回收。 这样解释不确定是否准确,请大神继续跟帖回答啊……
macro527 2017-04-13
  • 打赏
  • 举报
回复
不明白被释放了是啥情况,是销毁了吗?textur不应该已经被创建出来了吗?
地狱为王 2017-04-12
  • 打赏
  • 举报
回复
Onclick()运行完之后texture就被释放了吧
macro527 2017-04-12
  • 打赏
  • 举报
回复
可以实现,想知道为啥会这样呢,必须是成员变量吗?
兔子想吃人 2017-04-12
  • 打赏
  • 举报
回复
1.保证Onclick()函数可以被触发。 2.把texture当做成员变量 texture = new Texture2D(128, 128); image.GetComponent<Image>().material.mainTexture = texture; 这两句放在start里。

2,584

社区成员

发帖
与我相关
我的任务
社区描述
Unity3D相关内容讨论专区
游戏unity 技术论坛(原bbs)
社区管理员
  • Unity3D
  • 芝麻粒儿
  • 「已注销」
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

Unity3D社区公告:

  1. 社区致力于解决各种Unity3D相关的“疑难杂症”。
  2. 社区不允许发布与Unity3D或相关技术无关内容。
  3. 社区版主邀请各位一道为打造优秀社区不懈努力。

试试用AI创作助手写篇文章吧