- Режим игры - появление астероидов со словами (3d TMP) хорошее и показывает tmp. p>
- Сборка и запуск — в браузере WebGL при сборке и запуске астероиды появляются только с их изображением, TMP не отображается (связанный слово об астероиде).
Вот коды на 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();
SpriteRenderer asteroidSpriteRenderer = wordObj.GetComponent();
RectTransform canvasRectTransform = canvas.GetComponent();
RectTransform wordRectTransform = wordObj.GetComponent();
//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();
asteroidSpriteRenderer = GetComponent();
tmpWord = GetComponentInChildren();
campaignAudio = GameObject.FindGameObjectWithTag("Audio").GetComponent();
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.MainModule mainModule = explosionParticleSystem.main;
mainModule.startSize = asteroidScale * mainExplosionScaleFactor;
// If the explosion has child particles, scale them as well
ParticleSystem[] childParticleSystems = explosion.GetComponentsInChildren();
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);
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... ld-and-run