示例#1
0
文件: Editor.cs 项目: pipe01/WireEdit
        protected GameObject CreateWire(Transform a, Transform b, bool ghost = true)
        {
            if (WirePlacer.ConnectionExists(a.parent.gameObject, b.parent.gameObject))
            {
                return(null);
            }

            GameObject obj = GameObject.Instantiate(Prefabs.Wire);
            Wire       w;

            if (a.parent.tag == "Input" && b.parent.tag == "Input")
            {
                w        = obj.AddComponent <InputInputConnection>();
                w.Point1 = a;
                w.Point2 = b;
            }
            else
            {
                w = obj.AddComponent <InputOutputConnection>();

                if (a.parent.tag == "Input")
                {
                    w.Point1 = a;
                    w.Point2 = b;
                }
                else
                {
                    w.Point1 = b;
                    w.Point2 = a;
                }
            }

            if (!WirePlacer.CanConnect(w))
            {
                GameObject.Destroy(obj);
                return(null);
            }

            w.DrawWire();

            if (ghost)
            {
                obj.GetComponent <BoxCollider>().enabled = false;
                StuffPlacer.OutlineObject(obj, OutlineColor.blue);
            }
            else
            {
                w.SetPegsBasedOnPoints();
                StuffConnector.LinkConnection(w);
                StuffConnector.SetAppropriateConnectionParent(w);
                obj.AddComponent <ObjectInfo>().ComponentType = ComponentType.Wire;
                obj.GetComponent <BoxCollider>().enabled      = true;
            }

            return(obj);
        }
    public SnappingPeg GetPegToSnapTo()
    {
        Vector3    origin = Wire.GetWireReference(gameObject).position;
        RaycastHit hit;

        if (Physics.Raycast(origin, -Wire.GetWireReference(gameObject).forward, out hit, 0.20f, Wire.IgnoreWiresLayermask)) // snapped connections will be about 18cm long; we cast for 20, just to be safe
        {
            if (hit.collider.tag == "Input")
            {
                SnappingPeg OtherSnappyPeg = hit.collider.GetComponent <SnappingPeg>();
                if (OtherSnappyPeg != null)
                {
                    if (StuffConnector.CanConnect(gameObject, OtherSnappyPeg.gameObject) && !WirePlacer.ConnectionExists(gameObject, OtherSnappyPeg.gameObject) &&
                        hit.transform.InverseTransformPoint(hit.point).z < -0.49f // make sure it hits the right face of the other peg
                        &&

                        // make sure it's rotated approximately correctly
                        // use the wire reference instead of the peg itself so the same code works for vertical and horizontal pegs
                        ((Wire.GetWireReference(hit).eulerAngles.y + 180 > Wire.GetWireReference(transform).eulerAngles.y - 2 &&
                          Wire.GetWireReference(hit).eulerAngles.y + 180 < Wire.GetWireReference(transform).eulerAngles.y + 2)

                         || (Wire.GetWireReference(hit).eulerAngles.y - 180 > Wire.GetWireReference(transform).eulerAngles.y - 2 &&
                             Wire.GetWireReference(hit).eulerAngles.y - 180 < Wire.GetWireReference(transform).eulerAngles.y + 2)))
                    {
                        return(OtherSnappyPeg);
                    }
                }
            }
        }

        return(null);
    }