问题:
播放模式 - 带文字的小行星(3d TMP)的生成很好,它显示了 tmp。
构建并运行 - 在构建和运行的 WebGL 浏览器上,小行星仅生成其图像,不显示 TMP(小行星的相关单词)。
这是我们学校的顶点,我不是一个非常专业的程序员,请不要伤害我。我们计划使用 UNITY 2D 构建一款以小行星为主题的 2D 打字游戏。你正确地输入了小行星的单词,你就摧毁了它。问题就在这里。我有一个预制件,其中包含一个游戏对象(父级、精灵渲染器,用于显示小行星精灵)和一个子游戏对象(3D - TextMeshPro,用于显示单词)。在他们的脚本中,我减少了它们的排序层,以便较早生成的小行星会挡住新小行星的视线。 Parent的排序层当然不会更大,所以它不会阻塞Child。
以下是C#代码:
这是一个实例化预制件的脚本。 (用文字生成一颗小行星)
public class TutorialWordSpawner : MonoBehaviour
{
public GameObject wordPrefab;
public Canvas canvas;
public Sprite[] asteroidSprites;
public TutorialWordDisplay SpawnWord()
{
//References
GameObject wordObj = Instantiate(wordPrefab, canvas.transform);
TutorialWordDisplay wordDisplay = wordObj.GetComponentInChildren<TutorialWordDisplay>();
SpriteRenderer asteroidSpriteRenderer = wordObj.GetComponent<SpriteRenderer>();
RectTransform canvasRectTransform = canvas.GetComponent<RectTransform>();
RectTransform wordRectTransform = wordObj.GetComponent<RectTransform>();
//Assignments and Functionalities.
//Randomizes the position within the canvas (the spawning area's limit).
float x = Random.Range(0, canvasRectTransform.rect.width) - canvasRectTransform.rect.width / 2;
float y = Random.Range(0, canvasRectTransform.rect.height) - canvasRectTransform.rect.height / 2;
wordRectTransform.anchoredPosition = new Vector2(x, y);
asteroidSpriteRenderer.sprite = asteroidSprites[Random.Range(0, asteroidSprites.Length)];
return wordDisplay;
}
}
附加到实例化预制件子级的脚本,该子级具有 3d textmeshpro 组件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class TutorialWordDisplay : MonoBehaviour
{
public GameObject asteroidExplosion;
public TextMeshPro tmpWord;
public float scaleSpeed = 1f; // Speed at which the asteroid scales up
public int spawnIndex;
public TutorialWordManager wordManager;
public TutorialWord word;
public CampaignAudioScript campaignAudio;
private RectTransform asteroidRectTransform;
private SpriteRenderer asteroidSpriteRenderer;
private void Start()
{
asteroidRectTransform = GetComponent<RectTransform>();
asteroidSpriteRenderer = GetComponent<SpriteRenderer>();
tmpWord = GetComponentInChildren<TextMeshPro>();
campaignAudio = GameObject.FindGameObjectWithTag("Audio").GetComponent<CampaignAudioScript>();
asteroidSpriteRenderer.sortingLayerName = "Foreground";
asteroidSpriteRenderer.sortingOrder = spawnIndex-1;
tmpWord.sortingLayerID = SortingLayer.NameToID("Foreground");
tmpWord.sortingOrder = spawnIndex;
}
public void SetWord(string _word)
{
tmpWord.text = _word;
}
public void RemoveLetter()
{
tmpWord.text = tmpWord.text.Remove(0, 1);
tmpWord.color = Color.cyan;
}
public void RemoveTypedAsteroid()
{
DestroyAsteroidExplosion();
Destroy(gameObject);
}
public void RemoveMissedAsteroid()
{
wordManager.RemoveMissedAsteroid(word);
Destroy(gameObject);
}
private void Update()
{
ScaleAsteroid();
}
private void ScaleAsteroid()
{
// Increase the scale of the asteroid over time
asteroidRectTransform.localScale += Vector3.one * scaleSpeed * Time.deltaTime;
// Optionally, remove the object if it gets too large
if (asteroidRectTransform.localScale.x > 10f) // Adjust the threshold as needed
{
DestroyAsteroidExplosion();
RemoveMissedAsteroid();
}
}
private void DestroyAsteroidExplosion()
{
// Instantiate the explosion prefab
GameObject explosion = Instantiate(asteroidExplosion, transform.position, transform.rotation);
// Get the current scale of the asteroid
float asteroidScale = asteroidRectTransform.localScale.x; // Assuming uniform scaling
// Define the scaling factors for the explosion sizes
float mainExplosionScaleFactor = 0.8f; // Main explosion size (80% of the asteroid's size)
float childExplosionScaleFactor = 0.2f; // Child particles size (20% of the asteroid's size)
// Scale the main explosion particle system
ParticleSystem explosionParticleSystem = explosion.GetComponent<ParticleSystem>();
ParticleSystem.MainModule mainModule = explosionParticleSystem.main;
mainModule.startSize = asteroidScale * mainExplosionScaleFactor;
// If the explosion has child particles, scale them as well
ParticleSystem[] childParticleSystems = explosion.GetComponentsInChildren<ParticleSystem>();
foreach (ParticleSystem child in childParticleSystems)
{
ParticleSystem.MainModule childMainModule = child.main;
childMainModule.startSizeMultiplier = asteroidScale * childExplosionScaleFactor;
}
// Destroy the explosion after it plays
Destroy(explosion, explosionParticleSystem.main.duration);
// Play explosion sound effect
campaignAudio.PlaySFX(campaignAudio.asteroidExplosion);
}
}
当我发布并运行它时,我只在浏览器上查找控制台,然后我看到了错误。 脚本 (TutorialWordSpawner) 未初始化。顾名思义,该脚本负责生成小行星并用文字打包(即 TextMeshPro)。我修改了代码以使其初始化。
简而言之,如果遇到错误,请务必查看浏览器的控制台。
它是[F12]或者你可以在检查元素上看到它。