1. 创建一个存放障碍物和道具的集合
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PatternManager : MonoBehaviour {
public static PatternManager instance;
/// <summary>
/// 创建一个障碍物和道具的仓库,用来存放各种道具和障碍物,并在场景生成的时候随机生成
/// </summary>
public List<Pattern> Patterns = new List<Pattern>();
// Use this for initialization
void Start () {
instance = this;
}
}
/// <summary>
/// 一节场景上面放的物体的集合
/// </summary>
[System.Serializable]
public class Pattern
{
public List<PatternItem> PatternItems = new List<PatternItem>();
}
/// <summary>
/// 实例化的物体和位置
/// </summary>
[System.Serializable]
public class PatternItem
{
public GameObject gameobject;
public Vector3 position;
}
2. 手动调整场景的障碍物并用写的编辑器功能实现把所有添加在场景里面的障碍物和道具全部添加在上面的数组里面
using UnityEngine;
using System.Collections;
using UnityEditor;
public class PatternSystemEditor : EditorWindow {
/// <summary>
/// 添加到unity菜单上面
/// </summary>
[MenuItem("Window/AddPatternToSystem")]
static void AddPatternToSystem()
{
///找到障碍物和道具的集合所在的物体
var gameManager = GameObject.Find("GameManager");
if(gameManager !=null)
{
//获取到障碍物和道具的仓库
var patternManager = gameManager.GetComponent<PatternManager>();
//判断如果在unity 面板上选择的物体大于1
if (Selection.gameObjects.Length == 1)
{
//找到选择物体下面存放所有障碍物和道具的父物体
var item = Selection.gameObjects[0].transform.Find("Item");
if (item != null)
{
//初始化道具和障碍物的集合
Pattern pattern = new Pattern();
foreach (var child in item)
{
Transform childTransform = child as Transform;
if (childTransform != null)
{
//获取物体所在的prefab
var prefeb = UnityEditor.PrefabUtility.GetPrefabParent(childTransform.gameObject);
if (prefeb != null)
{
//给障碍物和道具的仓库添加新的成员
PatternItem patternItem = new PatternItem
{
gameobject = prefeb as GameObject,
position = childTransform.transform.localPosition
};
//添加数据
pattern.PatternItems.Add(patternItem);
}
}
}
patternManager.Patterns.Add(pattern);
}
}
}
}
}
3. 场景的不断更新和场景中道具的添加
using UnityEngine;
using System.Collections;
public class FloorSetter : MonoBehaviour {
/// <summary>
/// 角色正在哪一个路上
/// </summary>
public GameObject floorOnRunning;
/// <summary>
/// 需要在角色当前地板前面的地板
/// </summary>
public GameObject floorForward;
public static FloorSetter instance;
// Use this for initialization
void Start () {
instance = this;
}
/// <summary>
/// 删除当前地板
/// </summary>
/// <param name="floor"></param>
void RemoveItem(GameObject floor)
{
//找到需要实例化障碍物的父物体
var item = floor.transform.Find("Item");
if (item != null)
{
//循环删除当前地板上的所欲障碍物
foreach (var child in item)
{
Transform childTranform = child as Transform;
if (childTranform != null)
{
Destroy(childTranform.gameObject);
}
}
}
}
/// <summary>
/// 添加一个新的地板
/// </summary>
/// <param name="floor"></param>
void AddItem(GameObject floor)
{
//找到需要实例化障碍物的父物体
var item = floor.transform.Find("Item");
if (item != null)
{
//
var patternManager = PatternManager.instance;
if (patternManager != null && patternManager.Patterns!=null && patternManager.Patterns.Count>0)
{
//根据障碍物和道具的集合随机生成障碍物或者道具
var pattern = patternManager.Patterns[Random.Range(0, patternManager.Patterns.Count)];
if(pattern!=null && pattern.PatternItems!=null && pattern.PatternItems.Count>0)
{
//循环添加到场景地板上
foreach (var patternItem in pattern.PatternItems)
{
//新建障碍物或者道具
var newObj = Instantiate(patternItem.gameobject);
//障碍物和道具统一放在一个物体下面
newObj.transform.parent = item;
newObj.transform.localPosition = patternItem.position;
}
}
}
}
}
// Update is called once per frame
void Update () {
//判断角色的位置
//如果角色所在的位置大于角色所在的地板+地板的长度
if (transform.position.z > floorOnRunning.transform.position.z + 32)
{
//先删除
RemoveItem(floorOnRunning);
//再添加
AddItem(floorOnRunning);
//然后更新地板的坐标
floorOnRunning.transform.position = new Vector3(0, 0, floorForward.transform.position.z + 32);
//然后两个地板交替互换从而模拟跑酷场景不断生成
GameObject temp = floorOnRunning;
floorOnRunning = floorForward;
floorForward = temp;
}
}
}
G
M
T
Detect languageAfrikaansAlbanianAmharicArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CorsicanCroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchFrisianGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHawaiianHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanKurdishKyrgyzLaoLatinLatvianLithuanianLuxembourgishMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPashtoPersianPolishPortuguesePunjabiRomanianRussianSamoanScots GaelicSerbianSesothoShonaSindhiSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshXhosaYiddishYorubaZulu |
| AfrikaansAlbanianAmharicArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CorsicanCroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchFrisianGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHawaiianHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanKurdishKyrgyzLaoLatinLatvianLithuanianLuxembourgishMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPashtoPersianPolishPortuguesePunjabiRomanianRussianSamoanScots GaelicSerbianSesothoShonaSindhiSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshXhosaYiddishYorubaZulu |
|
|
|
|
|
Text-to-speech function is limited to 200 characters