예제 #1
0
    void Update()
    {
        if (realtime && Application.isPlaying && watch)
        {
            if (mat != watch.transform.localToWorldMatrix)
            {
                mat = watch.transform.localToWorldMatrix;
                MegaWirePlantPolesList plant = GetComponent <MegaWirePlantPolesList>();

                if (plant)
                {
                    plant.Rebuild();
                }
            }
        }
    }
    public override void OnInspectorGUI()
    {
        bool rebuild = false;
        MegaWirePlantPolesList mod = (MegaWirePlantPolesList)target;

        undoManager.CheckUndo();
        EditorGUIUtility.LookLikeControls();

        if (GUILayout.Button("Add Waypoint"))
        {
            Vector3 p = Vector3.zero;

            if (mod.waypoints.Count > 0)
            {
                if (mod.waypoints.Count > 1)
                {
                    p = mod.waypoints[mod.waypoints.Count - 1];
                    Vector3 p1 = mod.waypoints[mod.waypoints.Count - 2];
                    p += p - p1;
                }
                else
                {
                    p    = mod.waypoints[mod.waypoints.Count - 1];
                    p.x += 4.0f;
                }
            }

            mod.waypoints.Add(p);
            rebuild = true;
        }

        for (int i = 0; i < mod.waypoints.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("-", GUILayout.MaxWidth(20.0f)))
            {
                mod.waypoints.RemoveAt(i);
                rebuild = true;
            }
            else
            {
                mod.waypoints[i] = EditorGUILayout.Vector3Field("Waypoint " + i, mod.waypoints[i]);
            }
            EditorGUILayout.EndHorizontal();
        }

        if (mod.waypoints.Count > 0)
        {
            if (GUILayout.Button("Delete"))
            {
                mod.waypoints.RemoveAt(mod.waypoints.Count - 1);
                rebuild = true;
            }
        }

        mod.start   = EditorGUILayout.Slider("Start", mod.start, 0.0f, 1.0f);
        mod.length  = EditorGUILayout.Slider("Length", mod.length, 0.0f, 1.0f);
        mod.spacing = EditorGUILayout.FloatField("Spacing", mod.spacing);
        if (mod.spacing < 1.0f)
        {
            mod.spacing = 1.0f;
        }
        mod.closed = EditorGUILayout.Toggle("Closed", mod.closed);

        mod.pole = (GameObject)EditorGUILayout.ObjectField("Pole Obj", mod.pole, typeof(GameObject), true);

        mod.offset = EditorGUILayout.FloatField("Offset", mod.offset);
        mod.rotate = EditorGUILayout.Vector3Field("Rotate", mod.rotate);

        mod.conform = EditorGUILayout.BeginToggleGroup("Conform", mod.conform);
        mod.upright = EditorGUILayout.Slider("Upright", mod.upright, 0.0f, 1.0f);
        EditorGUILayout.EndToggleGroup();

        mod.material     = (Material)EditorGUILayout.ObjectField("Wire Material", mod.material, typeof(Material), true);
        mod.copyfrom     = (MegaWire)EditorGUILayout.ObjectField("Copy Wire", mod.copyfrom, typeof(MegaWire), true);
        mod.addwires     = EditorGUILayout.BeginToggleGroup("Add Wires", mod.addwires);
        mod.reverseWire  = EditorGUILayout.Toggle("Reverse Wire", mod.reverseWire);
        mod.wireSizeMult = EditorGUILayout.FloatField("Wire Size Mult", mod.wireSizeMult);
        mod.stretch      = EditorGUILayout.Slider("Stretch", mod.stretch, 0.0f, 1.5f);
        EditorGUILayout.EndToggleGroup();

        mod.seed = EditorGUILayout.IntField("Seed", mod.seed);
        mod.positionVariation = EditorGUILayout.Vector3Field("Position Variation", mod.positionVariation);
        mod.rotateVariation   = EditorGUILayout.Vector3Field("Rotate Variation", mod.rotateVariation);
        mod.spacingVariation  = EditorGUILayout.Slider("Spacing Variation", mod.spacingVariation, 0.0f, 1.0f);

        //mod.realtime = EditorGUILayout.Toggle("Realtime", mod.realtime);
        //mod.watch = (GameObject)EditorGUILayout.ObjectField("Watch", mod.watch, typeof(GameObject), true);

        if (GUI.changed || rebuild)
        {
            EditorUtility.SetDirty(target);
            mod.Rebuild();
        }

        GUI.changed         = false;
        mod.showgizmoparams = EditorGUILayout.Foldout(mod.showgizmoparams, "Gizmo Params");

        if (mod.showgizmoparams)
        {
            mod.showgizmo = EditorGUILayout.BeginToggleGroup("Show Gizmo", mod.showgizmo);
            mod.gizmoType = (MegaWireGizmoType)EditorGUILayout.EnumPopup("Show Type", mod.gizmoType);

            mod.units      = (MegaWireUnits)EditorGUILayout.EnumPopup("Units", mod.units);
            mod.unitsScale = EditorGUILayout.FloatField("Units Scale", mod.unitsScale);

            mod.arrowwidth  = EditorGUILayout.FloatField("Arrow Width", mod.arrowwidth);
            mod.arrowlength = EditorGUILayout.FloatField("Arrow Length", mod.arrowlength);
            mod.arrowoff    = EditorGUILayout.Slider("Arrow Offset", mod.arrowoff, 0.0f, 1.0f);
            mod.vertStart   = EditorGUILayout.FloatField("Vert Start", mod.vertStart);
            mod.vertLength  = EditorGUILayout.FloatField("Vert Length", mod.vertLength);

            mod.dashdist = EditorGUILayout.FloatField("Dash Dist", mod.dashdist);
            mod.lineCol  = EditorGUILayout.ColorField("Line Color", mod.lineCol);
            mod.arrowCol = EditorGUILayout.ColorField("Arrow Color", mod.arrowCol);
            mod.otherCol = EditorGUILayout.ColorField("Other Color", mod.otherCol);
            mod.dashCol  = EditorGUILayout.ColorField("Dash Color", mod.dashCol);
            EditorGUILayout.EndToggleGroup();
        }

        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }

        undoManager.CheckDirty();
    }
    void OnSceneGUI()
    {
        MegaWirePlantPolesList mod = (MegaWirePlantPolesList)target;

        arrow.arrowlength = mod.arrowlength;
        arrow.arrowoff    = mod.arrowoff;
        arrow.arrowwidth  = mod.arrowwidth;
        arrow.vertStart   = mod.vertStart;
        arrow.vertLength  = mod.vertLength;
        arrow.lineCol     = mod.lineCol;
        arrow.otherCol    = mod.otherCol;
        arrow.arrowcol    = mod.arrowCol;
        arrow.dashCol     = mod.dashCol;
        arrow.units       = MegaArrow.GetUnitsString(mod.units);
        arrow.unitsscale  = MegaArrow.GetUnitsScale(mod.units, mod.unitsScale);
        arrow.dashdist    = mod.dashdist;

        if (mod.showgizmo && (mod.gizmoType == MegaWireGizmoType.Waypoint || mod.gizmoType == MegaWireGizmoType.Both))
        {
            switch (Event.current.type)
            {
            case EventType.keyDown:
                if (Event.current.keyCode == KeyCode.A)
                {
                    if (!addmode)
                    {
                        Debug.Log("Start Add Mode");
                        addmode = true;
                        Event.current.Use();
                    }
                }
                break;

            case EventType.keyUp:
                if (Event.current.keyCode == KeyCode.A)
                {
                    if (addmode)
                    {
                        Debug.Log("End Add mode");
                        addmode     = false;
                        addingpoint = false;
                        Event.current.Use();
                    }
                }
                break;
            }

            if (mod.showgizmo && (mod.gizmoType == MegaWireGizmoType.Waypoint || mod.gizmoType == MegaWireGizmoType.Both))
            {
                Camera cam = UnityEditor.SceneView.currentDrawingSceneView.camera;

                if (cam)
                {
                    Vector3 camwp = Event.current.mousePosition;

                    camwp.z = 0.0f;

                    float dist = 100000.0f;

                    seg = -1;

                    // Just do distance from mid point, and use closest
                    for (int i = 0; i < mod.waypoints.Count - 1; i++)
                    {
                        Vector3 p  = mod.waypoints[i];
                        Vector3 p1 = mod.waypoints[i + 1];

                        Vector3 wp  = mod.transform.TransformPoint(p);
                        Vector3 wp1 = mod.transform.TransformPoint(p1);

                        Vector3 sp  = cam.WorldToScreenPoint(wp);
                        Vector3 sp1 = cam.WorldToScreenPoint(wp1);

                        sp.z  = 0.0f;
                        sp1.z = 0.0f;

                        Vector3 cp2 = (sp + sp1) * 0.5f;

                        float d = Vector3.Distance(camwp, cp2);
                        if (d < dist)
                        {
                            dist = d;
                            seg  = i;
                        }
                    }

                    if (seg >= 0)
                    {
                        Vector3 pos = Vector3.Lerp(mod.waypoints[seg], mod.waypoints[seg + 1], 0.5f);

                        Handles.color = Color.green;
                        Handles.DotCap(1066, mod.transform.TransformPoint(pos), Quaternion.identity, 0.2f);
                    }
                }
            }

            if (addmode)
            {
                int controlID = GUIUtility.GetControlID(FocusType.Passive);

                Camera cam = UnityEditor.SceneView.currentDrawingSceneView.camera;

                if (cam)
                {
                    switch (Event.current.GetTypeForControl(controlID))
                    {
                    case EventType.MouseDown:
                        GUIUtility.hotControl = controlID;
                        Event.current.Use();

                        if (!addingpoint)
                        {
                            addingpoint = true;

                            if (seg >= 0)
                            {
                                Vector3 cp = (mod.waypoints[seg] + mod.waypoints[seg + 1]) * 0.5f;
                                mod.waypoints.Insert(seg + 1, cp);
                            }
                        }

                        break;

                    case EventType.MouseUp:
                        addingpoint           = false;
                        GUIUtility.hotControl = 0;
                        Event.current.Use();
                        break;
                    }
                }
            }
        }

        // Draw the fancier lines
        // Show waypoints and or pole spacing
        // option to control units
        //string units = MegaArrow.GetUnitsString(mod.units);
        //float unitsscale = MegaArrow.GetUnitsScale(mod.units, mod.unitsScale);

        GUIStyle style = new GUIStyle();

        style.normal.textColor = mod.lineCol;

        if (mod.waypoints.Count > 1 && mod.showgizmo)
        {
            float arrowoff = mod.vertStart + (mod.vertLength * mod.arrowoff);
            arrow.arrowoff = arrowoff;

            if (mod.gizmoType == MegaWireGizmoType.Waypoint || mod.gizmoType == MegaWireGizmoType.Both)
            {
                for (int i = 0; i < mod.waypoints.Count; i++)
                {
                    Vector3 lp = mod.transform.TransformPoint(mod.waypoints[i]);
                    lp = SceneView.currentDrawingSceneView.camera.WorldToScreenPoint(lp);

                    Handles.color = mod.lineCol;
                    if (lp.z > 0.0f)
                    {
                        Handles.Label(mod.transform.TransformPoint(mod.waypoints[i]), "" + i, style);
                    }

                    if (i > 0)
                    {
                        Vector3 p1 = mod.transform.TransformPoint(mod.waypoints[i - 1]);
                        Vector3 p2 = mod.transform.TransformPoint(mod.waypoints[i]);
                        arrow.DrawArrow(i, p1, p2, style);
                    }

                    Vector3 p = mod.transform.worldToLocalMatrix.MultiplyPoint(Handles.PositionHandle(mod.transform.localToWorldMatrix.MultiplyPoint(mod.waypoints[i]), Quaternion.identity));
                    if (p != mod.waypoints[i])
                    {
                        mod.waypoints[i] = p;
                        mod.Rebuild();
                    }
                }

                if (mod.closed)
                {
                    Handles.DrawLine(mod.waypoints[mod.waypoints.Count - 1], mod.waypoints[0]);
                }
            }
            // Poles

            if (mod.gizmoType == MegaWireGizmoType.Pole || mod.gizmoType == MegaWireGizmoType.Both)
            {
                Handles.matrix = Matrix4x4.identity;

                for (int i = 0; i < mod.poles.Count; i++)
                {
                    Vector3 lp = mod.transform.TransformPoint(mod.poles[i].transform.position);
                    lp = SceneView.currentDrawingSceneView.camera.WorldToScreenPoint(lp);

                    if (i > 0)
                    {
                        Vector3 p1 = mod.poles[i - 1].transform.position;
                        Vector3 p2 = mod.poles[i].transform.position;
                        arrow.DrawArrow(i, p1, p2, style);
                    }
                }
            }
        }
    }