【问题标题】:Change image via in scriptable object in Unity通过 Unity 中的脚本对象更改图像
【发布时间】:2019-12-08 23:03:07
【问题描述】:

所以我正在尝试制作一款文字冒险游戏,并且我想在出现特定对话时更改游戏背景。我在开始时已经有一个黑色图像,并且想在整个游戏过程中更改它。

我为对话制作了一个可编写脚本的对象。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

[CreateAssetMenu(menuName = "Dialogue")]
public class Dialogue : ScriptableObject
{

[TextArea(10, 14)] [SerializeField] string dialogueText;
[SerializeField] string hintText;
[SerializeField] Dialogue[] nextDialogue;
[SerializeField] public Image background; // Thats the old Image that I want to change
[SerializeField] public Sprite newBackground; // Thats the new Background I want to put in Image


public bool dialogueHolder;

public void Awake()
{
    if (background != null)
    {
        background.sprite = newBackground; // this code is not working
    }
}
public string getHintText()
{
    return hintText;
}
public string GetDialogue()
{
    return dialogueText;
}

public Dialogue[] GetNextDialogue()
{
    return nextDialogue;
}

【问题讨论】:

  • 你似乎没有清楚地勾勒出你被困在哪里或到目前为止你已经尝试过什么

标签: c# image unity3d sprite


【解决方案1】:

就像 Bugfinder 评论的那样,您还没有说明出了什么问题以及到目前为止您已经尝试过什么。 但是,查看您的代码,我可以看到一些肯定会出错的地方。

首先,您在可编写脚本的对象中有一个Awake 方法。这本身不会做任何事情,因为 Awake(以及其他)仅在 MonoBehaviour 对象上调用。

出于逻辑原因,我会将方法重命名为“激活”之类的名称,然后从应该负责处理对话的管理器(DialogueManager?)脚本中调用它。

另外,我认为您错误地粘贴了代码。现在它肯定会产生编译错误。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

[CreateAssetMenu(menuName = "Dialogue")]
public class Dialogue : ScriptableObject
{

// these lines are duplicate

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

[CreateAssetMenu(menuName = "Dialogue")]
public class Dialogue : ScriptableObject
{

【讨论】:

  • 感谢您对我的帮助!
【解决方案2】:
private void ManageDialogue()
{
    var nextStates = startingDialogue.GetNextDialogue();

    for (int i = 0; i < nextStates.Length + 1; i++)
    {
            startingDialogue = nextStates[0];
    }
    if (animatedText.isActionPerformed == false) { animatedText.AnimateText(startingDialogue.GetDialogue()); }
    if (startingDialogue.background != null) { startingDialogue.background.sprite = startingDialogue.newBackground;}
}

所以这就是我将如何实现我的代码但是

startingDialogue.background.sprite = startingDialogue.newBackground

似乎对我不起作用。它一直是和以前一样的图像。

【讨论】:

  • 不是代码。图像很暗,我必须使它更亮才能看到图像。哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
  • 2022-10-08
  • 2015-12-13
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 2022-07-27
相关资源
最近更新 更多