Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以材料替换材料_C#_Unity3d - Fatal编程技术网

C# 以材料替换材料

C# 以材料替换材料,c#,unity3d,C#,Unity3d,如何使用脚本将精灵添加到播放器材质。我有一个商店菜单的球员,当一个喜欢选择一个精灵我喜欢添加到球员的材料,但我不知道如何做到这一点。我有我的代码,但只是这个我不知道如何添加。这是我的代码,我这样做,但不工作有人能告诉我如何添加playerSprite到playermaterial 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用UnityEngine.UI; 公共类ShopMenuew:单一行为 { 公共转换精神板; 公共

如何使用脚本将精灵添加到播放器材质。我有一个商店菜单的球员,当一个喜欢选择一个精灵我喜欢添加到球员的材料,但我不知道如何做到这一点。我有我的代码,但只是这个我不知道如何添加。这是我的代码,我这样做,但不工作有人能告诉我如何添加playerSprite到playermaterial

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类ShopMenuew:单一行为
{
公共转换精神板;
公共文本spriteBuySetText;
公共文本;
[序列化字段]
私人成本;
私有int-selectedSpriteIndex;
私有int-activeSpriteIndex;
//用于初始化
无效开始()
{
GameManager.Instance.state.gold=999;
UpdateGoldText();
InitShop();
onPriteSelect(GameManager.Instance.state.activeSprite);
SetSprite(GameManager.Instance.state.activeSprite);
}
//每帧调用一次更新
无效更新(){
}
私人店铺()
{
if(spritePanel==null)
Log(“您没有在inspector中指定sprite面板”);
int i=0;
foreach(spritePanel中的变换t)
{
int currentIndex=i;
按钮b=t.GetComponent();
b、 onClick.AddListener(()=>onPriteSelect(currentIndex));
图像img=t.GetComponent();
img.sprite=GameManager.Instance.isPriteOwned(i)?GameManager.Instance.PlayerPrite[currentIndex]:GameManager.Instance.PlayerPriteBuy[currentIndex];
i++;
}
}
私有void setprite(整数索引)
{ 
activeSpriteIndex=索引;
GameManager.Instance.state.activeSprite=索引;
GameManager.Instance.playerMaterial=GameManager.Instance.playerSprite[index];
spriteBuySetText.text=“当前”;
GameManager.Instance.Save();
}
私有void UpdateGoldText()
{
goldText.text=GameManager.Instance.state.gold.ToString();
}
PriteSelect上的专用void(int currentIndex)
{
Log(“选择颜色按钮:“+currentIndex”);
如果(selectedSpriteIndex==currentIndex)
回来
selectedSpriteIndex=当前索引;
if(GameManager.Instance.IsSpriteOwned(currentIndex))
{
如果(activeSpriteIndex==currentIndex)
{
spriteBuySetText.text=“当前”;
}
其他的
{
spriteBuySetText.text=“选择”;
}
}
其他的
{
spriteBuySetText.text=“购买:”+spriteCost[currentIndex].ToString();
}
}
PriteBuyset()上的公共无效
{
Log(“购买雪碧”);
if(GameManager.Instance.IsSpriteOwned(selectedSpriteIndex))
{
设置价格指数(选定的价格指数);
GameManager.Instance.Save();
}
其他的
{
if(GameManager.Instance.BuySprite(selectedSpriteIndex,spriteCost[selectedSpriteIndex]))
{
设置价格指数(选定的价格指数);
spritePanel.GetChild(selectedSpriteIndex.GetComponent().sprite=GameManager.Instance.playerSprite[selectedSpriteIndex];
UpdateGoldText();
}
其他的
{
Log(“没有足够的黄金”);
}
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类GameManager:MonoBehavior
{
公共静态GameManager实例{set;get;}
公共储蓄国家;
公共int currentSkinIndex=0;
公共国际货币=0;
公共int皮肤亲和性=1;
公共货币;
公共材料播放材料;
公共精灵[]玩家精灵;
公共精灵[]玩家Pritebuy;
公共纹理[]纹理;
私人空间()
{
DontDestroyOnLoad(游戏对象);
实例=此;
加载();
}
公共作废保存()
{
PlayerPrefs.SetString(“保存”,Helper.Serialize(state));
}
公共空荷载()
{
如果(PlayerPrefs.HasKey(“保存”))
{
state=Helper.Deserialize(PlayerPrefs.GetString(“save”);
}
其他的
{
state=新的SaveState();
Save();
Log(“未找到保存文件,正在创建新文件!”);
}
}
公共图书馆是私有的(国际索引)
{

return(state.spriteOwned&(1因此您的
playerMaterial
属性在这里实际上并不有用

它基本上是一张3x5索引卡,上面写着一本书的名字,它不是真正的书。所以当你这样做时:

GameManager.Instance.playerMaterial = GameManager.Instance.otherMaterial[index];
你正在删除索引卡上的书名,写上一本新书的书名,然后想知道为什么书架上的内容没有改变。它没有改变,因为你从来没有对书架做过任何事情

您需要更改播放器的
渲染器的
组件的材质属性,以便进行更改

例如,像

GameManager.Instance.player.getComponent<Renderer>().sharedMaterial = GameManager.Instance.otherMaterial[index];
GameManager.Instance.player.getComponent().sharedMaterial=GameManager.Instance.otherMaterial[index];

我可以用此代码将精灵添加到材质中吗公共精灵[]其他材质;@Toni@Toni当然不能。这是两种不同的类型。为了解决您的问题(与您的问题不同,应作为新问题发布)你必须解释你到底想做什么。你想把这些精灵中的一个分配给玩家吗?你想把这些精灵中的每一个分配给玩家的某个部分吗?你在做什么?把这些精灵中的一个分配给玩家吗?像商店菜单一样,看图片并在所有场景中保存精灵谢谢你使用[索引]。