怎么打开动态加载图片的读写权限
read/write Enable 无法在项目运行时更改
/// <summary>
/// 以IO方式进行加载
/// </summary>
private void LoadByIO()
{
string ImagePath = string.Format("{0}/{1}", Application.streamingAssetsPath, "Test.jpg");
Debug.LogFormat("ImagePath:{0}", ImagePath);
//创建文件读取流
FileStream fileStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//创建文件长度缓冲区
byte[] bytes = new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes, 0, (int)fileStream.Length);
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
fileStream = null;
//创建Texture
int width = 300;
int height = 372;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes);
Color[] colors = texture.GetPixels();
Debug.LogFormat("pixels:{0}", colors.Length);
}
看看能不能帮到你
加载为Texture格式就能读啊,动态加载的图片也是可以获取texture的
using Unity;
using System.Collections;
public class SendLine : MonoBehaviour {
public Vector3 targetPoint;
private Transform MyTransform;
public Texture2D TextureMap;
void Start ()
{
MyTransform = GetComponent<Transform>();
}
void Update ()
{
sendLineMeth();
}
void sendLineMeth()
{
RaycastHit hit;
if( Physics.Raycast(MyTransform.position, -MyTransform.up,out hit,10f))
{
targetPoint= hit.point;
Debug.DrawLine(MyTransform.position, targetPoint);
Vector2 pixelUV = hit.textureCoord;
pixelUV.x *= TextureMap.width;
pixelUV.y *= TextureMap.height;
TextureMap.SetPixel((int)pixelUV.x, (int)pixelUV.y,Color.red);
Debug.Log(pixelUV);
TextureMap.Apply();
}
}
}
在任何一个脚本里写这个
public Color color1;
public Texture2D a;
void Update() {
color1 = a.GetPixel(10, 20);
}
在Update每一帧都获取一次x10 y20位置的颜色
想获得多个点可以用 a.GetPixels();
找了很久最后在Unity社区上找到了原因,因为用的是AssetBundle加载资源,资源包是别人做的
所以原因就是打包资源包的同事没有勾上可读性,所以会报错!!
附上社区地址 https://answers.unity.com/questions/988174/create-modify-texture2d-to-readwrite-enabled-at-ru.html
最后 wjintao 给的方法都是可行的,因为外部加载的图片默认可读。(AssetBundle默认不行是因为unity有自动标记)