我有一个带有图像和一个TextMeshPro(TMP)的Canvas作为子级,并且对话框的canvas组件在Start()方法中设置为false,以便将其隐藏在主Canvas中。 TMP出现在图像上方(就像对话框中的文本一样)。我在2D环境中有一个播放器和一个硬币精灵。当玩家拿起硬币时,我尝试显示对话框和TMP,如下所示。
public class PlayerMovement : MonoBehaviour {
public GameObject suggestion;
public GameObject dialogBox;
private bool wasSuggestionShown; //to check if dialog was shown
private void Start()
{
wasSuggestionShown = false;
suggestionTimer = 0;
dialogBox.GetComponent<Canvas>().enabled = false; //To hide the dialog box
}
void Update () {
//horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input
if (wasSuggestionShown)
{
suggestionTimer += Time.deltaTime;
if (suggestionTimer > 5)
{
wasSuggestionShown = false;
dialogBox.GetComponent<Canvas>().enabled = false; //TO hide dialog box after displaying it
}
}
}
void FixedUpdate ()
{
// Move the character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
//For destroying coin object on collision with player
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("coin"))
{
//For destroying coin and to recude player movement speed.
Destroy(col.gameObject); //Coin Disappears
isRunSpeedReduced = true;
runSpeed = 10f;
//For Showing dialog box
dialogBox.GetComponent<Canvas>().enabled = true;
wasSuggestionShown = true;
}
}
}
在OnTriggerEnter2D()
方法中,我检查玩家角色是否触及硬币,如果触及,则销毁该对象并显示对话框和TMP,并在5秒钟后隐藏它们。问题是
”“当我包含另一个硬币时,相同的对话框和TMP不会在玩家拾取第二个硬币时显示。两个硬币都具有相同的标签'coin'“
可能会争辩说,如果脚本位于被破坏或处于非活动状态的同一对象中,那么这是不可能的。但是我要在附加到播放器对象的播放器移动脚本中完成所有这些操作。
此外,我切换对话框的方式也没有什么不同。是dialogBox.GetComponent<Canvas>().enabled = true;
还是dialogBox.SetActive(true)
这些显示仅显示一次,这是第一次出现。
即使我要实例化它,我也不知道将其正确放置在画布中的确切转换。 (我希望它位于中间的底部,就像如何锚定)
场景层次结构:
答案 0 :(得分:1)
问题出在您的 Update()
上,您在suggestionTimer
滴答之后关闭了画布:
void Update () {
//horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input
if (wasSuggestionShown)
{
suggestionTimer += Time.deltaTime;
if (suggestionTimer > 5)
{
wasSuggestionShown = false;
dialogBox.GetComponent<Canvas>().enabled = false; //TO hide dialog box after displaying it
}
}
}
原因是,您碰到新硬币时从未重置suggestionTimer
。这样做:
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("coin"))
{
//For destroying coin and to recude player movement speed.
Destroy(col.gameObject); //Coin Disappears
isRunSpeedReduced = true;
runSpeed = 10f;
//For Showing dialog box
dialogBox.GetComponent<Canvas>().enabled = true;
wasSuggestionShown = true;
// !!! ADD THIS
suggestionTimer = 0;
}
}
答案 1 :(得分:0)
问题是,在OnTriggerEnter2D
上设置了wasSuggestionShown = true
,然后在下一个Update方法中,您正在检查wasSuggestionShown
,发现它是正确的,然后翻转画布马上又关闭。
这可能会有所帮助。这只是十种方法中的一种,可以帮助您解决问题:
private bool _showSuggestion = true;
public bool showSuggestion
{
get { return _showSuggestion; }
set
{
if ( !value && _showSuggestion )
{
dialogBox.SetActive ( false );
_showSuggestion = value;
}
}
}
void Update () {
//horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input
if ( showSuggestion )
{
suggestionTimer += Time.deltaTime;
if ( suggestionTimer > 5f ) showSuggestion = false;
}
}
上面的代码将一直等到您第一次将showSuggestion
设置为false时,然后再忽略进一步打开建议的尝试。我怀疑那是您想要的吗?如果我读错了情况,则可以对代码进行一些微调,以获取正确的行为。