C# 在我的记忆卡游戏中上传时,如何允许精灵图像保持不变?
我正在Unity2d中玩一个记忆卡游戏。基本上,用户将不得不获得8场比赛,以使他们赢得比赛。我的游戏功能齐全,我正在努力让它充满活力。我可以上传自己的图片并将其加载到游戏中。但是,游戏中的图像没有正确调整大小 这是我上传到游戏中的图片 这就是我想要的照片 我用于上传的代码是C# 在我的记忆卡游戏中上传时,如何允许精灵图像保持不变?,c#,unity3d,C#,Unity3d,我正在Unity2d中玩一个记忆卡游戏。基本上,用户将不得不获得8场比赛,以使他们赢得比赛。我的游戏功能齐全,我正在努力让它充满活力。我可以上传自己的图片并将其加载到游戏中。但是,游戏中的图像没有正确调整大小 这是我上传到游戏中的图片 这就是我想要的照片 我用于上传的代码是 using System; using System.Collections; using System.Collections.Generic; using System.IO; using SFB; using Un
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using SFB;
using UnityEngine;
public class OpenFileHelper : MonoBehaviour
{
public static List<Texture2D> LoadImage()
{
var extensions = new[] {
new ExtensionFilter("Image Files", "png", "jpg", "jpeg" )
};
var paths = StandaloneFileBrowser.OpenFilePanel("Select Images", "", extensions, true);//StandaloneFileBrowser.OpenFilePanel("Select Images", "", extensions, true);
//System.IO.File.ReadAllLines(Application.persistentDataPath + "/Settings.txt")
var textureList = new List<Texture2D>();
foreach (var path in paths)
{
byte[] imageByte = File.ReadAllBytes(path);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(imageByte);
textureList.Add(texture);
}
//textureList.Resize(60, 60);
//textureList.Apply();
return textureList;
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
使用SFB;
使用UnityEngine;
公共类OpenFileHelper:MonoBehavior
{
公共静态列表LoadImage()
{
var扩展=新[]{
新的ExtensionFilter(“图像文件”、“png”、“jpg”、“jpeg”)
};
var path=StandaloneFileBrowser.OpenFilePanel(“选择图像”,扩展名,true);//StandaloneFileBrowser.OpenFilePanel(“选择图像”,扩展名,true);
//System.IO.File.ReadAllLines(Application.persistentDataPath+“/Settings.txt”)
var textureList=新列表();
foreach(路径中的变量路径)
{
byte[]imageByte=File.ReadAllBytes(路径);
纹理2d纹理=新纹理2d(2,2);
加载图像(imageByte);
纹理列表。添加(纹理);
}
//textureList.Resize(60,60);
//textureList.Apply();
返回纹理列表;
}
}
我在GitHub中使用了这个家伙创建的自定义包装:
我用来将图像转换为sprite的代码如下:
.
.
.
public List<Texture2D> textures;
.
.
private float width;
private float height;
.
.
void initializeCards()
{
OpenFile();
.
.
}
.
.
public void OpenFile()
{
textures = OpenFileHelper.LoadImage();
if (!_init)
{
_init = true;
}
}
.
.
public Sprite getCardFace(int i)
{
width = 101;
height = 180;
Sprite sp = Sprite.Create(textures[i - 1], new Rect(0, 0, width, height), new Vector2(0.5f, 0.0f), 1.0f);
return sp;
}
。
.
.
公共列表纹理;
.
.
私有浮动宽度;
私人浮动高度;
.
.
void initializeCards()
{
OpenFile();
.
.
}
.
.
公共void OpenFile()
{
纹理=OpenFileHelper.LoadImage();
如果(!\u init)
{
_init=真;
}
}
.
.
公共Sprite getCardFace(int i)
{
宽度=101;
高度=180;
Sprite sp=Sprite.Create(纹理[i-1],新矩形(0,0,宽度,高度),新矢量2(0.5f,0.0f),1.0f);
返回sp;
}
我尝试在我的精灵中制作纹理。宽度和纹理。高度。创建代码。然而,当我使用这些,我会得到CS1061错误
任何建议都将不胜感激 井纹理
是多个纹理2d
的,实际上没有宽度
或高度
。但是单身汉有;)
你可能应该使用
public Sprite getCardFace(int i)
{
var tex = textures[i - 1];
var sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.one * 0.5f, 1.0f);
return sp;
}
哦,谢谢你@derHugo这就像一个符咒