В настоящее время я создаю 2D-игру, вдохновленную обеими статьями, пожалуйста, а не моим соседом, в данный момент у меня есть персонаж, который появляется на моем экране и предназначен для предоставления мне документов, которые я затем могу просмотреть, но я не смог этого сделать. Я перепробовал так много разных вещей, что мой код больше не работает, поэтому мне понадобится помощь в написании сценария.
Вот код, который у меня сейчас есть:
Код: Выделить всё
using System.Collections.Generic;
using UnityEngine;
public class DocumentManager : MonoBehaviour
{
[Header("Assign document prefabs (different ID cards, etc.)")]
public GameObject[] documentPrefabs;
[Header("Parent for spawned docs (optional)")]
public Transform container;
[Header("How many to spawn (will clamp to number of prefabs)")]
public int spawnCount = 3;
// Keep references so we don't spawn duplicates
private readonly List spawned = new List();
// Call this when character reaches center
public void ShowDocuments()
{
if (spawned.Count > 0) return; // already spawned
if (documentPrefabs == null || documentPrefabs.Length == 0) return;
Camera cam = Camera.main;
if (cam == null)
{
Debug.LogError("No main camera found.");
return;
}
// center world point — using a z that is the same plane as your docs (0)
Vector3 screenCenter = new Vector3(Screen.width / 2f, Screen.height / 2f, Mathf.Abs(cam.transform.position.z));
Vector3 worldCenter = cam.ScreenToWorldPoint(screenCenter);
worldCenter.z = 0f;
// choose how wide the documents spread (units)
float spread = 1.2f;
int toSpawn = Mathf.Clamp(spawnCount, 1, documentPrefabs.Length);
// spawn evenly around center
int midIndex = toSpawn / 2;
for (int i = 0; i < toSpawn; i++)
{
int prefabIndex = i % documentPrefabs.Length;
Vector3 pos = worldCenter + new Vector3((i - midIndex) * spread, 0f, 0f);
GameObject prefab = documentPrefabs[prefabIndex];
GameObject go = Instantiate(prefab, pos, Quaternion.identity, container);
spawned.Add(go);
}
}
// Removes and destroys spawned documents
public void ClearDocuments()
{
foreach (var g in spawned) if (g != null) Destroy(g);
spawned.Clear();
}
// Convenience: spawn from inspector
#if UNITY_EDITOR
[UnityEditor.MenuItem("Tools/SpawnDocs_Debug")]
private static void DebugSpawn() { }
#endif
}

Подробнее здесь: https://stackoverflow.com/questions/798 ... e-words-ap
Мобильная версия