在Unity3d中的运行时将PNG图像作为纹理加载 [英] Loading a PNG image as a texture at runtime in Unity3d

查看:919
本文介绍了在Unity3d中的运行时将PNG图像作为纹理加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一系列PNG图像作为精灵,以在平面上为Unity中的增强现实应用程序创建动画.将PNG图像作为纹理加载,然后将纹理应用于平面以创建动画序列,以增强现实目标进行跟踪.

I'm using a series of PNG images as sprites to create animations on a plane for an augmented reality application in Unity. The PNG images are loaded as textures, and the textures are applied to a plane to create an animation sequence that tracks with an augmented reality target.

这是附着在平面上并控制动画序列的脚本.

Here is the script that is attached to the plane and controls the animation sequence.

#pragma strict
var tecture : Texture[];

var changeInterval : float = 0.06;
var isRunning = false;
var isLooping = false;

function Start () {
    var isRunning = true;
}

function Update () {
    if( isRunning == true) {
        Animation ();
    }
}

function Animation () {
    var index : int = Time.time/changeInterval;
    index = index % tecture.Length;
    renderer.material.mainTexture = tecture[index];
    if ( index == tecture.Length-1){
        if( isLooping == false){
            isRunning = false;
        }
           renderer.material.mainTexture = tecture[0];
    }
}

通过在编辑器中拖放PNG文件来指定纹理,如果我限制PNG数量,一切都可以正常工作.但是,随着纹理数量的增加,我逐渐摆脱了Android上的内存错误.

Textures are assigned by drag-and-drop of PNG files in the editor and everything works fine if I limit the PNG count. As my texture count increases, however, I'm getting out of memory errors on Android.

我想在运行时将PNG文件作为纹理加载,以限制内存数量,但是我无法使代码正常工作.

I would like to load the PNG files as textures at run time to limit the number memory, but I'm having trouble getting the code working.

我尝试过LoadImage.

I've tried LoadImage.

var imageTextAsset : TextAsset = Resources.Load("test.png");
var tex = new Texture2D (4, 4);
tex.LoadImage(imageTextAsset.bytes);
renderer.material.mainTexture = tex;

我还尝试过直接将图像作为纹理加载(因为这似乎可以与编辑器一起使用).

I've also tried loading the image directly as a texture (since this seems to work with the editor).

renderer.material.mainTexture = Resources.LoadAssetAtPath"Assets/Images/test.png",Texture);

两个都不运气.有关如何实现此目标的任何建议?

No luck with either. Any suggestions on how to accomplish this?

已编辑

由于Xerosigma提供的链接,我能够使它正常工作.我在使用路径时遇到了问题(我怀疑是在加载时需要使用Texture).

I was able to get this working thanks to the link provided by Xerosigma. I had an issue with paths (and I suspect with the Texture requirement on load).

除了加载PNG纹理外,我还发现Unity不对纹理进行内存管理.要恢复内存,使用后需要手动卸载纹理.

Outside of loading the PNG textures, I also discovered that Unity does not do memory management on textures. To recover memory, textures need to be manually unloaded after use.

所有纹理均放置在资源"区域中.文件夹.资源文件夹可以放置在Assets层次结构中的任何位置-我为每个PNG序列创建了几个单独的文件夹来组织它们,然后在每个PNG序列中添加一个Resources文件夹来保存实际的PNG.

Textures are all placed in a "Resources" folder. The resources folder can be placed anywhere in the Assets hierarchy - I created several individual folders for each of my PNG sequences to organize them, then added a Resources folder within each to hold the actual PNGs.

通过指定纹理计数(即100),基本名称(例如"texture_")和零填充来通过编辑器分配纹理.

Textures are assigned through the editor by specifying a texture count (ie. 100), a base name ("texture_" for instance), and zero padding.

#pragma strict

var imageCount : int = 0;
var imageNameBase : String = "";
var imageNameZeroPadding : int = 0;

var changeInterval : float = 0.06;
var isRunning = false;
var isLooping = false;

private var texture : Texture = null;

function PlayRocket () {
    isRunning = true;
}

function Start () {
    var isRunning = false;
    var fileName : String = imageNameBase + ZeroPad(0,imageNameZeroPadding);
    texture = Resources.Load(fileName);
}

function Update () {
    if( isRunning == true) {
        PNGAnimation ();
    }
}

function PNGAnimation () {
    var index : int = Time.time/changeInterval;
    index = index % imageCount;
    var fileName : String = imageNameBase + ZeroPad(index,imageNameZeroPadding);
    Resources.UnloadAsset(texture);
    texture = Resources.Load(fileName);
    renderer.material.mainTexture = texture;


    if ( index == imageCount-1){
        Debug.Log("End of Animation");
        Debug.LogWarning("End of Animation");
        if( isLooping == false){
            isRunning = false;
        }
        fileName = imageNameBase + ZeroPad(0,imageNameZeroPadding);
        Resources.UnloadAsset(texture);
        texture = Resources.Load(fileName);
        renderer.material.mainTexture = texture;
    }
}

function ZeroPad(number : int, size : int) {
    var numberString : String = number.ToString();
    while (numberString.Length < size) numberString = "0" + numberString;
    return numberString;
}

推荐答案

在C#中:

// Load all textures into an array
Object[] textures = Resources.LoadAll("Textures", typeof(Texture2D));

// Load a single texture
Texture2D texture = Resources.Load("Texture") as Texture2D;

renderer.material.mainTexture = texture;

希望它会有所帮助.您应该能够将其转换为JS没问题.只需查看Unity文档.

Hope it helps. You should be able to convert it into JS no problem. Just look at the Unity documentation.

http://docs.unity3d.com/Documentation/ScriptReference/Resources.html

这篇关于在Unity3d中的运行时将PNG图像作为纹理加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆