Ejemplo n.º 1
0
    private void OnSectorLoaded(object source, EventArgs args)
    {
        StreamingScript str = source as StreamingScript;

        // Generate a random number of agents
        int x = UnityEngine.Random.Range(0, 50);

        for (int i = 0; i < x; i++)
        {
            // Randomize a location
            float   newX      = UnityEngine.Random.Range(-125, 125) + str.transform.position.x;
            float   newZ      = UnityEngine.Random.Range(-125, 125) + str.transform.position.z;
            Vector3 targetPos = new Vector3(newX, 0, newZ);

            // Raycast up/down to find the ground level, move agent to this point
            RaycastHit hit;
            if (Physics.Raycast(targetPos, transform.TransformDirection(Vector3.up), out hit, Mathf.Infinity))
            {
                targetPos = hit.point;
            }
            else if (Physics.Raycast(targetPos, transform.TransformDirection(Vector3.down), out hit, Mathf.Infinity))
            {
                targetPos = hit.point;
            }

            // Instantiate the bot and add it to the list of agents
            GameObject tempA = Instantiate(AIPrefab, targetPos, new Quaternion(0, 0, 0, 0), str.transform);
            agents.Add(tempA);
        }
    }
Ejemplo n.º 2
0
    void Save()
    {
        int i = 0;

        StreamingScript currentSector;

        // Check if an object is selected
        if (Selection.activeGameObject == null)
        {
            Debug.LogError("No sector was selected");
            return;
        }

        currentSector = Selection.activeGameObject.GetComponent(typeof(StreamingScript)) as StreamingScript;

        // Check if the object selected is a sector
        if (currentSector == null)
        {
            Debug.LogError("Current selection is not a sector");
            return;
        }

        ObjectList rawData = new ObjectList();
        Object     tempObject;

        string dataString;
        string path = Path.Combine(Application.streamingAssetsPath, (currentSector.name + ".json"));


        foreach (Transform child in currentSector.transform)
        {
            EditorUtility.DisplayProgressBar("Save Sector", "Process " + i, (float)i / currentSector.transform.childCount);
            if (child.GetComponent <ResourceName>())
            {
                tempObject            = new Object();
                tempObject.Type       = "Object";
                tempObject.PrefabName = child.GetComponent <ResourceName>().globalName;
                tempObject.Position   = child.position;
                tempObject.Rotation   = child.rotation.eulerAngles;
                tempObject.Scale      = child.localScale;

                rawData.List.Add(tempObject);
            }

            i++;
        }

        EditorUtility.ClearProgressBar();

        dataString = JsonUtility.ToJson(rawData);

        File.WriteAllText(path, dataString);

        Debug.Log("Sector saved to " + currentSector.name + ".json");


        return;
    }
Ejemplo n.º 3
0
    void Load()
    {
        // Check if an object is selected
        if (Selection.activeGameObject == null)
        {
            Debug.LogError("No sector was selected");
            return;
        }

        currentSector = Selection.activeGameObject.GetComponent(typeof(StreamingScript)) as StreamingScript;

        // Check if the object selected is a sector
        if (currentSector == null)
        {
            Debug.LogError("Current selection is not a sector");
            return;
        }

        UnityEngine.Object tempResource;
        GameObject         tempObject;

        string path = Path.Combine(Application.streamingAssetsPath, (currentSector.name + ".json"));

        // Load the terrain
        TerrainData tdata = (TerrainData)Resources.Load("t_" + currentSector.name);
        GameObject  tempT = Terrain.CreateTerrainGameObject(tdata);

        tempT.transform.parent        = currentSector.transform;
        tempT.transform.localPosition = new Vector3(-125, -10, -125);

        // If an object file exists, Load it
        if (File.Exists(path))
        {
            string data = File.ReadAllText(path);
            ObjectsList = JsonUtility.FromJson <ObjectList>(data);
            foreach (Object o in ObjectsList.List)
            {
                if (o.Type == "Object")
                {
                    tempResource = Resources.Load(o.PrefabName);
                    if (tempResource == null)
                    {
                        Debug.LogError("Resource not found: " + o.PrefabName);
                    }
                    else
                    {
                        tempObject = (GameObject)Instantiate(tempResource, o.Position, Quaternion.Euler(o.Rotation), currentSector.transform);
                        tempObject.transform.localScale = o.Scale;
                    }
                }
            }
        }
        else
        {
            Debug.Log("No list found");
        }
    }
Ejemplo n.º 4
0
    private void OnSectorUnloaded(object source, EventArgs args)
    {
        StreamingScript tempStreamingScript = source as StreamingScript;
        GameObject      tempSector          = tempStreamingScript.gameObject;
        GameObject      tempChild;

        int numOfChildren = tempSector.transform.childCount - 1;

        for (int i = numOfChildren; i > -1; i--)
        {
            tempChild = tempSector.transform.GetChild(i).gameObject;

            if (agents.Contains(tempChild))
            {
                agents.Remove(tempChild);
                Destroy(tempChild);
            }
        }
    }
Ejemplo n.º 5
0
    void OnGUI()
    {
        string title;

        if (Selection.activeGameObject != null)
        {
            currentSector = Selection.activeGameObject.GetComponent(typeof(StreamingScript)) as StreamingScript;
        }

        // Check if an object is selected
        if (Selection.activeGameObject == null)
        {
            title = "null";
        }
        // Check if the object selected is a sector
        else if (currentSector == null)
        {
            title = "null";
        }
        else
        {
            title = currentSector.name;
        }

        GUILayout.Label("Current sector: " + title, EditorStyles.boldLabel);

        if (GUILayout.Button("Load Sector"))
        {
            Load();
        }

        if (GUILayout.Button("Save Sector"))
        {
            Save();
        }
    }