That's the question, here i have the code where i generate both, Networkmanager and the dinamicaly generated characters.
Idealy, i want to have one of this dinamicaly generated characters, but i failed to make it work with the NetworkManager, mostly because of the "prefab" requeriment, i don't want to save a prefab to save them and then dinamize them, is there any way i can archieve this?
Here is the code:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
public class CharacterSpriteCreator : NetworkBehaviour {
// Use this for initialization
private Dictionary DicCharacterSprite;
GameObject newGameObject;
void Start () {
newGameObject = CreateCharacter (new Vector3 (5f, 0f, 0f), "CharacterPlus", "Character2");
GameObject go = new GameObject("NetworkManager");
go.AddComponent ().playerSpawnMethod = PlayerSpawnMethod.RoundRobin;
go.GetComponent ().playerPrefab = Instantiate(newGameObject);
go.AddComponent ();
GameObject startingPoint = new GameObject("startingPoint");
startingPoint.transform.position = new Vector3 (50,0,1);
startingPoint.transform.parent = go.transform;
startingPoint.AddComponent ();
GameObject startingPoint2 = new GameObject("startingPoint2");
startingPoint2.transform.position = new Vector3 (-50,0,1);
startingPoint2.transform.parent = go.transform;
startingPoint2.AddComponent ();
//GameObject.Find ("Head").AddComponent ("ThisIsYou");
}
// Update is called once per frame BUT HERE IS NOT IN USE!!!
Dictionary LoadSprites(){
DicCharacterSprite = new Dictionary ();
Sprite[] sprites = Resources.LoadAll ("Sprites");
//Debug.Log ("LOADED RESOURCE: CharacterSprite");
Debug.Log ("Speak Your Mind! (LOADED SPRITES)");
foreach (Sprite sprs in sprites) {
Debug.Log (sprs);
DicCharacterSprite [sprs.name] = sprs;
}
return DicCharacterSprite;
}
GameObject CreateCharacter(Vector3 vecAwake, string doango, string name){
DicCharacterSprite = LoadSprites ();
System.Type type = System.Type.GetType (doango);
GameObject go = new GameObject(name);
go.transform.SetParent (this.transform, true);
//go.transform.position = vecAwake;
go.name = doango;
GameObject head = new GameObject ("Head");
GameObject body = new GameObject ("Body");
head.AddComponent ().sprite = DicCharacterSprite["ThisIsYou"];
body.AddComponent ().sprite = DicCharacterSprite["RandyWeb"];
head.transform.localPosition = new Vector3(head.transform.position.x+.225f, (head.transform.position.y + 1.15f), head.transform.position.z);
body.transform.localScale = new Vector3(0.6f,0.6f,0);
head.transform.SetParent (go.transform);
body.transform.SetParent (go.transform);
go.AddComponent ().gravityScale = 0;
go.AddComponent (type);
go.AddComponent ().localPlayerAuthority = true;
return go;
}
}
Any constructive comment, request for clarification or suggestion would be very welcome too.
thanks in advance
↧