小编Maj*_*ajs的帖子

Unity:将 Sprite 保存/加载为 Json

我正在遵循一个教程,其中我需要保存属于某个对象的精灵。我正在创建一个项目数据库,当然希望该项目附带正确的图像。这就是我构建项目数据库的方式:

项目对象

[System.Serializable]
public class ItemObject{

    public int id;
    public string title;
    public int value;
    public string toolTip;
    public bool stackable;
    public string category;
    public string slug;
    public Sprite sprite;


    public ItemObject(int id, string title, int value, string toolTip, bool stackable, string category, string slug)
    {
        this.id = id;
        this.title = title;
        this.value = value;
        this.toolTip = toolTip;
        this.stackable = stackable;
        this.category = category;
        this.slug = slug;
        sprite = Resources.Load<Sprite>("items/" + slug);
    }
Run Code Online (Sandbox Code Playgroud)

项目数据

[System.Serializable]
public class ItemData {

    public …
Run Code Online (Sandbox Code Playgroud)

c# json unity-game-engine

5
推荐指数
1
解决办法
9889
查看次数

合并2个包含匹配ID的对象的列表

我有2个包含如下对象的列表:

清单1:

{'name': 'Nick', 'id': '123456'}
Run Code Online (Sandbox Code Playgroud)

清单2:

{'address': 'London', 'id': '123456'}
Run Code Online (Sandbox Code Playgroud)

现在,我要创建第三个列表,其中包含如下所示的对象:

{'name': 'Nick', 'address': 'London', 'id': '123456'}
Run Code Online (Sandbox Code Playgroud)

即,我想找到匹配的ID,并合并这些对象。

python

3
推荐指数
1
解决办法
39
查看次数

使用 UnityAction 传递参数

尝试发送 UnityAction 作为我的方法之一的参数,如下所示:

public void PopulateConversationList(string [] fullConversation, string onLastPagePrompt, string npcName, int stage, UnityAction action)
{
    conversations.Add(new Conversation(fullConversation, onLastPagePrompt, npcName, stage, action));
}

dialogHolder.PopulateConversationList(stage1, "Okay", _name, 1, QuestManager.Instance().ActivateQuest);
Run Code Online (Sandbox Code Playgroud)

这工作正常,但现在我想将以下操作作为参数传递:

public void ActivateQuest(int questId)
{
    Debug.Log("This is the id: " + questId);
}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用具有参数的操作时,它将不起作用:

dialogHolder.PopulateConversationList(stage1, "Okay", _name, 1, QuestManager.Instance().ActivateQuest(2));
Run Code Online (Sandbox Code Playgroud)

上面给出了错误:Cannot convert from void to UnityAction。如何将带有参数的 UnityAction 作为参数传递?

Action在对话中这样称呼:

dialog.OnAccept(ConvList[i].onLastPagePrompt, () =>
{
    ConvList[i].action();
    dialog.Hide();
});
Run Code Online (Sandbox Code Playgroud)

编辑:我最终采用的解决方案:

enter dialogHolder.PopulateConversationList(stage1, "Okay", _name, 1, () =>
    {
        QuestManager.Instance().ActivateQuest(0);
    }); …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

2
推荐指数
1
解决办法
3万
查看次数

调用 OnTriggerStay() 时检查按键

我有一个 NPC,当玩家对撞机与 NPC 碰撞时,我的玩家可以与之交谈,我使用这段代码来做到这一点:

private void OnTriggerStay2D(Collider2D other)
{
    if (other.gameObject.tag == "InteractiveArea")
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            Debug.Log("PRESSED NPC");
            CreateAndShowDialog();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,这真的是随机调用的,有时是我第一次按“E”,有时是第二次或第三次,等等。

我的刚体:

在此处输入图片说明

在此处输入图片说明

我使用的碰撞器是标准的 BoxCollider2D,我的玩家碰撞器是触发器,NPC 不是。

为什么在OnTriggerStay功能中没有检测到某些按键?

c# unity-game-engine unity5

1
推荐指数
1
解决办法
1257
查看次数

标签 统计

c# ×3

unity-game-engine ×3

json ×1

python ×1

unity5 ×1