通过 Unity 中的可编写脚本的对象更改图像
Change image via in scriptable object in Unity
所以我正在尝试制作一款文字冒险游戏,并且我想在出现特定对话时更改游戏的背景。我一开始就有一个黑色图像,想在整个游戏过程中更改它。
我已经为对话制作了一个可编写脚本的对象。
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;
}
就像 Bugfinder 评论的那样,您没有说明问题出在哪里以及到目前为止您已经尝试过什么。
但是,查看您的代码,我可以看出一些肯定会出错的地方。
首先,您在可编写脚本的对象中有一个 Awake
方法。这本身不会做任何事情,因为 Awake
(以及其他)仅在 MonoBehaviour
对象上调用。
出于逻辑原因,我会将该方法重命名为 "Activate" 之类的名称,然后从负责处理对话的管理器 (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
{
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
似乎对我不起作用。它一直是和以前一样的图像。
所以我正在尝试制作一款文字冒险游戏,并且我想在出现特定对话时更改游戏的背景。我一开始就有一个黑色图像,想在整个游戏过程中更改它。
我已经为对话制作了一个可编写脚本的对象。
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;
}
就像 Bugfinder 评论的那样,您没有说明问题出在哪里以及到目前为止您已经尝试过什么。 但是,查看您的代码,我可以看出一些肯定会出错的地方。
首先,您在可编写脚本的对象中有一个 Awake
方法。这本身不会做任何事情,因为 Awake
(以及其他)仅在 MonoBehaviour
对象上调用。
出于逻辑原因,我会将该方法重命名为 "Activate" 之类的名称,然后从负责处理对话的管理器 (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
{
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
似乎对我不起作用。它一直是和以前一样的图像。