/// <summary> /// 迭代解析游戏对象索引 /// </summary> /// <param name="gameObjectIndex">游戏对象索引</param> void IterationParseGameObjectIndex(List <GameObjectIndex> gameObjectIndex) { for (int i = 0; i < gameObjectIndex.Count; i++) { GameObjectIndex children = gameObjectIndex[i]; EditorGUI.indentLevel = children.hierarchy; { GUIStyle style = new GUIStyle("Foldout"); style.fontStyle = FontStyle.Bold; Color myStyleColor = new Color(0.2985075f, 0.4923412f, 1f); style.normal.textColor = myStyleColor; style.onNormal.textColor = myStyleColor; style.hover.textColor = myStyleColor; style.onHover.textColor = myStyleColor; style.focused.textColor = myStyleColor; style.onFocused.textColor = myStyleColor; style.active.textColor = myStyleColor; style.onActive.textColor = myStyleColor; children.foldout = EditorGUILayout.Foldout(children.foldout, children.gameObject.name, style); if (children.foldout) { //解析组件 ParseComponents(children); //迭代 IterationParseGameObjectIndex(children.children); } } } }
/// <summary> /// 显示父级游戏对象 /// </summary> void ShowParentGameObject() { GameObject gameObject = EditorGUILayout.ObjectField(rootLabel, parentGameObject, typeof(GameObject), true) as GameObject; //更改 预设父级游戏对象 if (parentGameObject != gameObject) { parentGameObject = gameObject; //重置 父级游戏对象索引 parentGameObjectIndex = null; } EditorGUILayout.Space(); if (parentGameObject == null) { //获取选中游戏对象 parentGameObject = Selection.activeGameObject; } //没预设父级 不执行下面的 if (parentGameObject == null) { //提示 EditorGUILayout.LabelField(tips); } }
/// <summary> /// 获取游戏对象索引 /// </summary> /// <param name="gameObject">游戏对象</param> /// <param name="parentHierarchy">父级层级</param> /// <returns></returns> GameObjectIndex GetGameObjectIndex(GameObject gameObject, int parentHierarchy, string path) { GameObjectIndex prefabIndex = new GameObjectIndex(); prefabIndex.gameObject = gameObject; prefabIndex.hierarchy = parentHierarchy + 1; Component[] components = gameObject.GetComponents <Component>(); for (int i = 0; i < components.Length; i++) { Component component = components[i]; ComponentIndex activeComponent = new ComponentIndex(); activeComponent.component = component; activeComponent.name = GetName(activeComponent.nameFormat, prefabIndex, activeComponent); activeComponent.path = path; prefabIndex.components.Add(activeComponent); allComponentIndex.Add(activeComponent); } return(prefabIndex); }
//Constructor public PlayerGameObject(Vector2 position, GameObjectIndex container, PlayerRegistry players, InputManager inputManager, KeyboardFormat format) : base(position, new BoxCollider(position, HITBOX_SIZE), container) { //Initialize keyboard input source keyboardInput = new PlayerKeyboardInputSource(this, format); //Register in subsystems players.AddPlayer(this); inputManager.registerInputSource(keyboardInput); //Set subsystem references this.players = players; this.input = inputManager; //Get player images images = new List <CanvasBitmap>(); images.Add(ImageManager.getImageByName("red_ghost_up")); images.Add(ImageManager.getImageByName("red_ghost_right")); images.Add(ImageManager.getImageByName("red_ghost_down")); images.Add(ImageManager.getImageByName("red_ghost_left")); //Initialize image Image = images[1]; //Initialize movement array movement = new bool[4] { false, false, false, false }; //Initialize direction Direction = CardinalDirection.EAST; }
string GetName(NameFormat nameFormat, GameObjectIndex gameObjectIndex, ComponentIndex componentIndex) { string name = string.Empty; switch (nameFormat) { case NameFormat.gameObject: name = gameObjectIndex.gameObject.name; break; case NameFormat.component: name = componentIndex.component.GetType().Name; break; case NameFormat.gameObject_component: name = gameObjectIndex.gameObject.name + "_" + componentIndex.component.GetType().Name; break; case NameFormat.path: name = gameObjectIndex.path; break; case NameFormat.path_component: name = gameObjectIndex.path + "_" + componentIndex.component.GetType().Name; break; default: break; } name = name.Replace(" ", ""); return(name); }
/// <summary> /// 迭代获取对象上的索引 /// </summary> /// <param name="prefabIndexs"></param> /// <param name="transform"></param> /// <param name="path"></param> /// <param name="variableName"></param> void IterationGetIndex(Transform transform, GameObjectIndex Parent, string path) { for (int i = 0; i < transform.childCount; i++) { Transform child = transform.GetChild(i); if (child.parent != parentGameObject.transform) { path = string.Format("{0}/{1}", path, child.gameObject.name); } else { path = child.gameObject.name; } GameObjectIndex prefabIndex = GetGameObjectIndex(child.gameObject, Parent.hierarchy, path); if (child.parent != parentGameObject.transform) { prefabIndex.path = Parent.path + "_" + child.name; } else { prefabIndex.path = child.name; } Parent.children.Add(prefabIndex); //迭代 IterationGetIndex(child, prefabIndex, path); } }
//Constructor public EnemyGameObject(Vector2 position, GameObjectIndex objectIndex, Random randomNumbers) : base(position, new BoxCollider(position, ENEMY_SIZE), objectIndex) { //Set the random number generator this.randomNumbers = randomNumbers; //Initialize animation frames List <CanvasBitmap> upAnimationFrames = new List <CanvasBitmap>() { ImageManager.getImageByName("small_pacman_closed"), ImageManager.getImageByName("small_pacman_halfopen_up"), ImageManager.getImageByName("small_pacman_open_up"), ImageManager.getImageByName("small_pacman_halfopen_up") }; List <CanvasBitmap> rightAnimationFrames = new List <CanvasBitmap>() { ImageManager.getImageByName("small_pacman_closed"), ImageManager.getImageByName("small_pacman_halfopen_right"), ImageManager.getImageByName("small_pacman_open_right"), ImageManager.getImageByName("small_pacman_halfopen_right") }; List <CanvasBitmap> downAnimationFrames = new List <CanvasBitmap>() { ImageManager.getImageByName("small_pacman_closed"), ImageManager.getImageByName("small_pacman_halfopen_down"), ImageManager.getImageByName("small_pacman_open_down"), ImageManager.getImageByName("small_pacman_halfopen_down") }; List <CanvasBitmap> leftAnimationFrames = new List <CanvasBitmap>() { ImageManager.getImageByName("small_pacman_closed"), ImageManager.getImageByName("small_pacman_halfopen_left"), ImageManager.getImageByName("small_pacman_open_left"), ImageManager.getImageByName("small_pacman_halfopen_left") }; //Initialize animations upAnimation = new Animation(upAnimationFrames, ANIMATION_LENGTH); rightAnimation = new Animation(rightAnimationFrames, ANIMATION_LENGTH); downAnimation = new Animation(downAnimationFrames, ANIMATION_LENGTH); leftAnimation = new Animation(leftAnimationFrames, ANIMATION_LENGTH); PlayAnimations(); //Setup animations array animations = new Animation[4] { upAnimation, rightAnimation, downAnimation, leftAnimation }; //Initialize to a random direction direction = new CardinalDirection(randomNumbers.Next(0, 4)); //Pick the matching animation OnDirectionChange(); //Initialize state state = ENEMY_STATE.IDLE; //Set the action timer ResetActionTimer(); }
//Constructor public BulletGameObject(Vector2 position, GameObjectIndex objectIndex, double lifetime, double speed) : base(position, new BoxCollider(position, BULLET_SIZE), objectIndex) { //Set properties Lifetime = lifetime; Speed = speed; //Set image Image = ImageManager.getImageByName("bullet"); }
/// <summary> /// 解析组建 /// </summary> /// <param name="prefabIndex"></param> void ParseComponents(GameObjectIndex prefabIndex) { for (int i = 0; i < prefabIndex.components.Count; i++) { ComponentIndex activeComponent = prefabIndex.components[i]; if (activeComponent.component.GetType() == typeof(Transform)) { continue; } EditorGUILayout.BeginHorizontal(); { GUIStyle style = new GUIStyle("TextField"); if (activeComponent.nameIsError) { Color myStyleColor = Color.red; style.normal.textColor = myStyleColor; style.onNormal.textColor = myStyleColor; style.hover.textColor = myStyleColor; style.onHover.textColor = myStyleColor; style.focused.textColor = myStyleColor; style.onFocused.textColor = myStyleColor; style.active.textColor = myStyleColor; style.onActive.textColor = myStyleColor; } string name = EditorGUILayout.TextField(string.Format(componentText, activeComponent.component.GetType().Name), activeComponent.name, style); if (activeComponent.name != name) { name = name.Replace(" ", ""); activeComponent.name = name; //修改后 遍历一次名字 CheckName(); } if (advanced) { NameFormat nameFormat = (NameFormat)EditorGUILayout.EnumPopup(nameFormatText, activeComponent.nameFormat); if (activeComponent.nameFormat != nameFormat) { activeComponent.nameFormat = nameFormat; activeComponent.name = GetName(nameFormat, prefabIndex, activeComponent); } } EditorGUILayout.EndHorizontal(); } } }
private void canvas_CreateResources(ICanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args) { //Initialize image loader with canvas //TODO: The image manager may fail for multiple frames ImageManager.Initialize(sender); //Initialize game systems drawIndex = new DrawableIndex(); collisions = new CollisionManager(); gameObjects = new GameObjectIndex(drawIndex, collisions); inputManager = new InputManager(); players = new PlayerRegistry(); //Load the required images ImageManager.LoadImages(PacmanImagePaths.Images); //Register key event listeners Window.Current.CoreWindow.KeyDown += canvas_KeyDown; Window.Current.CoreWindow.KeyUp += canvas_KeyUp; //Create a player player = new PlayerGameObject(new Vector2(200, 300), gameObjects, players, inputManager, KeyboardFormat.WASD); gameObjects.registerGameObject(player); //Create a dummy game object DummyGameObject testingObject = new DummyGameObject(new Vector2(100, 100), new Vector2(45, 45), gameObjects); //Register the new object in the game object index gameObjects.registerGameObject(testingObject); //Register the new object as an input listener inputManager.registerInputSource(new PlayerKeyboardInputSource(testingObject, KeyboardFormat.ARROWS)); //Enable debug drawing drawIndex.SetDebugDrawing(true); //Add a wall WallGameObject wall = new WallGameObject(new Vector2(100, 100), new Vector2(150, 200)); gameObjects.registerGameObject(wall); WallGameObject wall2 = new WallGameObject(new Vector2(150, 200), new Vector2(350, 250)); gameObjects.registerGameObject(wall2); WallGameObject wall3 = new WallGameObject(new Vector2(300, 100), new Vector2(350, 200)); gameObjects.registerGameObject(wall3); //Add an enemy object EnemyGameObject enemy = new EnemyGameObject(new Vector2(300, 300), gameObjects, new Random()); enemy.Target = player; gameObjects.registerGameObject(enemy); }
//Constructor public SpriteGameObject(Vector2 position, Collider collider, GameObjectIndex container) : base(position, collider) { //Initialize container this.container = container; //Initialize drawable image drawableImage = new DrawableImage(null, position, true); //Mark as alive Alive = true; }
void SetIndexs(List <GameObjectIndex> prefabIndexs, NameFormat nameType) { for (int i = 0; i < prefabIndexs.Count; i++) { GameObjectIndex children = prefabIndexs[i]; for (int j = 0; j < children.components.Count; j++) { ComponentIndex component = children.components[j]; component.nameFormat = nameType; component.name = GetName(nameType, children, component); } SetIndexs(children.children, nameType); } }
//Constructor public DummyGameObject(Vector2 position, Vector2 colliderSize, GameObjectIndex container) : base(position, new BoxCollider(position, colliderSize), container) { //Load the animation frames List <CanvasBitmap> frames = new List <CanvasBitmap>(); frames.Add(ImageManager.getImageByName("pacman_closed")); frames.Add(ImageManager.getImageByName("pacman_halfopen_right")); frames.Add(ImageManager.getImageByName("pacman_open_right")); frames.Add(ImageManager.getImageByName("pacman_halfopen_right")); animation = new Animation(frames, 200); animation.Play(); //Initialize motion array movement = new bool[4] { false, false, false, false }; }
public void Add(int key, GameObject go, bool destoryOld = false) { for (int i = 0; i < this.objectArray.Length; i++) { if (this.objectArray[i] != null && this.objectArray[i].id == key) { if (destoryOld) { UnityEngine.Object.DestroyImmediate(this.objectArray[i].gameObject); } this.objectArray[i].gameObject = go; return; } } GameObjectIndex[] tmpArray = new GameObjectIndex[objectArray.Length + 1]; Array.Copy(objectArray, tmpArray, objectArray.Length); objectArray = tmpArray; objectArray[objectArray.Length - 1] = new GameObjectIndex(key, go); }
/// <summary> /// 获取父级游戏对象索引 /// </summary> void GetParentGameObjectIndexData() { if (parentGameObject == null) { return; } if (parentGameObjectIndex != null) { return; } parentGameObjectIndex = new GameObjectIndex(); allComponentIndex = new List <ComponentIndex>(); IterationGetIndex(parentGameObject.transform, parentGameObjectIndex, string.Empty); CheckName(); }
void SetIndexs(List <GameObjectIndex> prefabIndexs, bool index) { for (int i = 0; i < prefabIndexs.Count; i++) { GameObjectIndex children = prefabIndexs[i]; for (int j = 0; j < children.components.Count; j++) { ComponentIndex component = children.components[j]; if (index == false) { component.name = string.Empty; } else { component.name = GetName(component.nameFormat, children, component); } } SetIndexs(children.children, index); } }
private void canvas_CreateResources(ICanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args) { //Initialize image loader with canvas //TODO: The image manager may fail for multiple pages ImageManager.Initialize(sender); //Initialize random number generator randomNumbers = new Random(); //Initialize game systems drawIndex = new DrawableIndex(); collisions = new CollisionManager(); gameObjects = new GameObjectIndex(drawIndex, collisions); inputManager = new InputManager(); players = new PlayerRegistry(randomNumbers); //Load the required images ImageManager.LoadImages(PacmanImagePaths.Images); //Register key event listeners Window.Current.CoreWindow.KeyDown += canvas_KeyDown; Window.Current.CoreWindow.KeyUp += canvas_KeyUp; //Add background image DrawableImage background = new DrawableImage(ImageManager.getImageByName("background1"), Vector2.Zero, false); drawIndex.AddDrawable(background); //Create players player = new PlayerGameObject(new Vector2(200, 300), gameObjects, players, inputManager, KeyboardFormat.WASD); gameObjects.registerGameObject(player); player2 = new PlayerGameObject(new Vector2(250, 300), gameObjects, players, inputManager, KeyboardFormat.ARROWS); gameObjects.registerGameObject(player2); //Create a dummy game object //DummyGameObject testingObject = new DummyGameObject(new Vector2(100, 100), new Vector2(45, 45), gameObjects); //Register the new object in the game object index //gameObjects.registerGameObject(testingObject); //Register the new object as an input listener //Disable debug drawing drawIndex.SetDebugDrawing(false); //Add walls WallGameObject wall = new WallGameObject(new Vector2(0, 0), new Vector2(40, 500)); gameObjects.registerGameObject(wall); WallGameObject wall2 = new WallGameObject(new Vector2(0, 0), new Vector2(850, 20)); gameObjects.registerGameObject(wall2); WallGameObject wall3 = new WallGameObject(new Vector2(830, 0), new Vector2(850, 500)); gameObjects.registerGameObject(wall3); WallGameObject wall4 = new WallGameObject(new Vector2(0, 480), new Vector2(850, 500)); gameObjects.registerGameObject(wall4); WallGameObject wall5 = new WallGameObject(new Vector2(185, 165), new Vector2(200, 330)); gameObjects.registerGameObject(wall5); WallGameObject wall6 = new WallGameObject(new Vector2(185, 165), new Vector2(685, 180)); gameObjects.registerGameObject(wall6); WallGameObject wall7 = new WallGameObject(new Vector2(670, 165), new Vector2(685, 500)); gameObjects.registerGameObject(wall7); WallGameObject wall8 = new WallGameObject(new Vector2(345, 320), new Vector2(525, 330)); gameObjects.registerGameObject(wall8); //Add an enemy object /*EnemyGameObject enemy = new EnemyGameObject(new Vector2(700, 120), gameObjects, new Random()); * enemy.Target = player; * gameObjects.registerGameObject(enemy); * * * EnemyGameObject enemy1 = new EnemyGameObject(new Vector2(400, 200), gameObjects, new Random()); * enemy1.Target = player; * gameObjects.registerGameObject(enemy1); * * EnemyGameObject enemy2 = new EnemyGameObject(new Vector2(250, 300), gameObjects, new Random()); * enemy2.Target = player; * gameObjects.registerGameObject(enemy2); * * EnemyGameObject enemy3 = new EnemyGameObject(new Vector2(100, 350), gameObjects, new Random()); * enemy3.Target = player; * gameObjects.registerGameObject(enemy3); * * EnemyGameObject enemy4 = new EnemyGameObject(new Vector2(750, 400), gameObjects, new Random()); * enemy4.Target = player; * gameObjects.registerGameObject(enemy4);*/ }