Example #1
0
    public void AddObject(GameObject go) //Adds an object to the management system. Automatically adds an ObjectID script, as well as a rigidbody and collider
    {
        Debug.Log("Adding Object!");
        if (go == null)
        {
            Debug.Log("ERROR: OBJECT is NULL!");
            return;
        }
        if (objectList == null)
        {
            objectList = new ArrayList();
        }
        objectList.Add(go);
        go.transform.parent = transform;
        if (go.GetComponent <ProcSection>())
        {
            Destroy(go.GetComponent <ProcSection>());
        }
        if (go.GetComponent <ProcShape>())
        {
            Destroy(go.GetComponent <ProcShape>());
        }
        if (objectList.Count > actionLimit) // limits how many objects to track
        {
            objectList.RemoveAt(objectList.Count - 1);
        }

        ObjectID oid = go.GetComponent <ObjectID>();

        if (oid == null)
        {
            oid = go.AddComponent <ObjectID>();
        }
        oid.SetId(count);
        ++count;

        Rigidbody rb = go.GetComponent <Rigidbody>();

        if (rb == null)
        {
            rb = go.AddComponent <Rigidbody>();
        }
        rb.useGravity             = gravity;
        rb.isKinematic            = !gravity;
        rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
        if (go.GetComponent <MeshCollider>())
        {
            go.GetComponent <MeshCollider>().convex = gravity;
        }
        else
        {
            go.AddComponent <MeshCollider>();
        }
        var editor = go.GetComponent <MeshEditor>();

        //        if (editor != null) editor.GenerateHandles();



        if (go.GetComponent <MeshFilter>() != null && oid.OutlineRenderer == null)
        {
            var outline = new GameObject();
            outline.transform.parent     = go.transform;
            outline.transform.position   = go.transform.position;
            outline.transform.rotation   = go.transform.rotation;
            outline.transform.localScale = new Vector3(1f, 1f, 1f);
            var filt = outline.AddComponent <MeshFilter>();
            var rend = outline.AddComponent <MeshRenderer>();
            rend.material           = OutlineMaterial;
            rend.material.color     = Color.yellow;
            filt.mesh               = go.GetComponent <MeshFilter>().mesh;
            oid.OutlineRenderer     = rend;
            rend.enabled            = false;
            rend.transform.position = rend.transform.parent.position;
        }

        // prevents overflow (lol if someone instantiates more than 4 billion objects they deserve a crash)
        if (count > int.MaxValue)
        {
            count = 0;
        }
    }