将 PNG 图像加载到数组并用作纹理
Load PNG images to array and use as textures
我一直在尝试在 Unity 中使用 PNG 图像作为纹理,当我只使用一两个图像时,很容易将它们拖放到检查器中。在我当前的项目中,我尝试将 300 多张图像加载到数组中,然后我想在每次更新时更改纹理,使其看起来像视频。
这是我目前的情况:
using UnityEngine;
using System.Collections;
public class ChangeImage : MonoBehaviour {
public Texture[] frames;
public int CurrentFrame;
public object[] images;
void OnMouseDown() {
if (GlobalVar.PlayClip == false){
GlobalVar.PlayClip = true;
} else {
GlobalVar.PlayClip = false;
}
}
public void Start() {
images = Resources.LoadAll("Frames");
for (int i = 0; i < images.Length; i++){
Texture2D texImage = (Texture2D) images[i];
frames[i] = texImage;
}
}
// Update is called once per frame
void Update () {
if(GlobalVar.PlayClip == true){
CurrentFrame++;
CurrentFrame %= frames.Length;
GetComponent<Renderer>().material.mainTexture = frames[CurrentFrame];
}
}
}
我一直在尝试将图像加载到对象数组中,将它们转换为纹理,然后输出到纹理数组。有谁知道我哪里出错了它似乎没有给出任何错误但纹理没有改变?
非常感谢任何建议
谢谢
你做的有点慢而且不合适。
我推荐的是使用 Animator 和动画。将所有纹理放入图集纹理中,这样您将限制绘制调用量。将该纹理设为精灵并使用精灵编辑器切入子精灵。
添加动画师并创建动画。 Select 整个子精灵并将它们拖入动画中。完成。
现在您可以通过动画器组件轻松控制速度和播放。
我最终设法解决了这个问题,问题似乎是资产加载工作不正常,这就是框架没有改变的原因。我还将包含图像的文件夹的名称从 "Frames" 更改为 "Resources"。这是为任何需要它的人提供的完整代码:
using UnityEngine;
using System.Collections;
public class ChangeImage : MonoBehaviour {
public Texture[] frames;
public int CurrentFrame;
void OnMouseDown() {
if (GlobalVar.PlayClip == false){
GlobalVar.PlayClip = true;
} else {
GlobalVar.PlayClip = false;
}
}
public void Start() {
frames = Resources.LoadAll<Texture>("");
}
// Update is called once per frame
void Update () {
if(GlobalVar.PlayClip == true){
CurrentFrame %= frames.Length;
CurrentFrame++;
Debug.Log ("Current Frame is " + CurrentFrame);
GetComponent<Renderer>().material.mainTexture = frames[CurrentFrame];
}
}
}
感谢您对动画的建议,我仍然会研究它,因为图像的性能不是很好。
我一直在尝试在 Unity 中使用 PNG 图像作为纹理,当我只使用一两个图像时,很容易将它们拖放到检查器中。在我当前的项目中,我尝试将 300 多张图像加载到数组中,然后我想在每次更新时更改纹理,使其看起来像视频。
这是我目前的情况:
using UnityEngine;
using System.Collections;
public class ChangeImage : MonoBehaviour {
public Texture[] frames;
public int CurrentFrame;
public object[] images;
void OnMouseDown() {
if (GlobalVar.PlayClip == false){
GlobalVar.PlayClip = true;
} else {
GlobalVar.PlayClip = false;
}
}
public void Start() {
images = Resources.LoadAll("Frames");
for (int i = 0; i < images.Length; i++){
Texture2D texImage = (Texture2D) images[i];
frames[i] = texImage;
}
}
// Update is called once per frame
void Update () {
if(GlobalVar.PlayClip == true){
CurrentFrame++;
CurrentFrame %= frames.Length;
GetComponent<Renderer>().material.mainTexture = frames[CurrentFrame];
}
}
}
我一直在尝试将图像加载到对象数组中,将它们转换为纹理,然后输出到纹理数组。有谁知道我哪里出错了它似乎没有给出任何错误但纹理没有改变?
非常感谢任何建议
谢谢
你做的有点慢而且不合适。
我推荐的是使用 Animator 和动画。将所有纹理放入图集纹理中,这样您将限制绘制调用量。将该纹理设为精灵并使用精灵编辑器切入子精灵。
添加动画师并创建动画。 Select 整个子精灵并将它们拖入动画中。完成。
现在您可以通过动画器组件轻松控制速度和播放。
我最终设法解决了这个问题,问题似乎是资产加载工作不正常,这就是框架没有改变的原因。我还将包含图像的文件夹的名称从 "Frames" 更改为 "Resources"。这是为任何需要它的人提供的完整代码:
using UnityEngine;
using System.Collections;
public class ChangeImage : MonoBehaviour {
public Texture[] frames;
public int CurrentFrame;
void OnMouseDown() {
if (GlobalVar.PlayClip == false){
GlobalVar.PlayClip = true;
} else {
GlobalVar.PlayClip = false;
}
}
public void Start() {
frames = Resources.LoadAll<Texture>("");
}
// Update is called once per frame
void Update () {
if(GlobalVar.PlayClip == true){
CurrentFrame %= frames.Length;
CurrentFrame++;
Debug.Log ("Current Frame is " + CurrentFrame);
GetComponent<Renderer>().material.mainTexture = frames[CurrentFrame];
}
}
}
感谢您对动画的建议,我仍然会研究它,因为图像的性能不是很好。