コード例 #1
0
    private void UpdateCurrentBlock(Camera currentCamera)
    {
        // Get the mouse position and initialise the rotation
        Vector3 mousePosition = Input.mousePosition;

        mousePosition.z = 5;

        // Move the dragged block for the next frame
        dragCollisionBlock.transform.position = currentCamera.ScreenToWorldPoint(mousePosition);
        dragCollisionBlock.transform.rotation = currentRotation;

        // Check if the dragged block is hitting any pips
        PipCollider collider = pipColliders.FirstOrDefault(p => (p.GetOtherPip() != null));
        GameObject  otherPip = collider?.GetOtherPip();

        if (otherPip != null)
        {
            GameObject pip = collider.gameObject;

            // Snap the rotation and get the other pips position
            Quaternion snappedPipRotation = SnapRotation(pip.transform.rotation, otherPip.transform.rotation);

            Quaternion pipRotation = dragCollisionBlock.transform.rotation * Quaternion.Inverse(pip.transform.rotation);
            Vector3    pipOffset   = Quaternion.Inverse(dragCollisionBlock.transform.rotation) * (pip.transform.position - dragCollisionBlock.transform.position);

            snappedRotation = pipRotation * snappedPipRotation;
            feedbackBlock.transform.position = otherPip.transform.position - (feedbackBlock.transform.rotation * pipOffset);
        }
        else
        {
            snappedRotation = dragCollisionBlock.transform.rotation;
            feedbackBlock.transform.position = dragCollisionBlock.transform.position;
        }

        // Update the outline
        feedbackOutline.OutlineColor = CheckValidPosition() ? ValidPositionColour : InvalidPositionColour;
    }
コード例 #2
0
    private void CreateEditorBlock()
    {
        GameObject go = Instantiate(dummyObject.EditorMesh);

        go.name = dummyObject.BlockName + "_Editor";
        go.tag  = "EditorBlock";

        var animator = go.GetComponent <Animator>();

        if (animator)
        {
            DestroyImmediate(animator);
        }

        Outline outline = go.AddComponent <Outline>();

        outline.OutlineColor = Color.cyan;
        outline.OutlineWidth = 5;

        go.AddComponent <RootGameObject>();

        Block block      = go.AddComponent <Block>();
        var   blockClass = typeof(Block);

        blockClass.GetField("blockType", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).
        SetValue(block, dummyObject.BlockType);     // Set via reflection as it's private

        FeedbackEditorBlock feedback = go.AddComponent <FeedbackEditorBlock>();

        feedback.FadeInSpeed     = 0.5f;
        feedback.FadeOutSpeed    = 0.2f;
        feedback.MouseOverColour = Color.cyan;
        feedback.SelectedColour  = Color.white;

        // Add a dummy pip
        GameObject pip = new GameObject("pip");

        pip.tag   = "EditorPip";
        pip.layer = LayerMask.NameToLayer("Pips");

        pip.transform.parent        = go.transform;
        pip.transform.localPosition = Vector3.zero;
        pip.transform.localRotation = Quaternion.identity;

        Rigidbody rb = pip.AddComponent <Rigidbody>();

        rb.useGravity  = false;
        rb.isKinematic = true;

        BoxCollider collider = pip.AddComponent <BoxCollider>();

        collider.isTrigger = true;
        collider.size      = new Vector3(0.5f, 0.5f, 0.25f);

        PipCollider pipCollider = pip.AddComponent <PipCollider>();

        pipCollider.TagType = Tags.EDITOR_PIP;

        Pip pipScript = pip.AddComponent <Pip>();

        pipScript.PipType = PipType.GENERAL;

        // Add the main block collider
        GameObject mainCollider = new GameObject("collider");

        mainCollider.tag = "EditorBlock";

        mainCollider.transform.parent        = go.transform;
        mainCollider.transform.localPosition = Vector3.zero;
        mainCollider.transform.localRotation = Quaternion.identity;
        mainCollider.transform.localScale    = new Vector3(0.95f, 0.95f, 0.95f);

        MeshCollider mainMeshCollider = mainCollider.AddComponent <MeshCollider>();

        mainMeshCollider.convex    = true;
        mainMeshCollider.isTrigger = true;

        if (dummyObject.SimplifiedMesh != null)
        {
            MeshFilter meshFilter = dummyObject.SimplifiedMesh.GetComponent <MeshFilter>();
            mainMeshCollider.sharedMesh = meshFilter.sharedMesh;
        }
        else
        {
            MeshFilter meshFilter = go.GetComponent <MeshFilter>();
            mainMeshCollider.sharedMesh = meshFilter.sharedMesh;
        }

        Rigidbody colliderRb = mainCollider.AddComponent <Rigidbody>();

        colliderRb.useGravity  = false;
        colliderRb.isKinematic = true;

        mainCollider.AddComponent <BlockCollider>();
    }