Unity Tips & Tricks 3

確認済のバージョン: 4.6

-

難易度: 初級

In this session, we will present an assortment of tips and tricks to use while working with Unity. These tips and tricks could make our experience in Unity better, faster, stronger or they could just keep us amused. We could simply be reminded of the obvious or we could be presented with something we've never heard of, thought of or imagined. Like entering a flea market or wild bazaar, we will hunt around and try to find something for everyone, even if everything we present isn't useful to all. Tutor: Adam Buckner

Unity Tips & Tricks 3

初級 Tips

In this specific session we covered Inspector Tips, including Component Tools, the Documentation Quick Reference, the Context Menu, Reset Functions, Menu Item Attributes, a script to Align with Ground, additional useful attributes and an open Q&A session.

AlignWithGround

Code snippet

using UnityEngine;
using UnityEditor;
using System.Collections

public class AlignWithGround : MonoBehaviour {
    [MenuItem ("Tools/Transform Tools/Align with ground %t")]
    static void AlignWithGround () {
        Transform [] transforms = Selection.transforms;
        foreach (Transform myTransform in transforms) {
            RaycastHit hit;
            if (Physics.Raycast (myTransform.position, -Vector3.up, out hit)) {
                Vector3 targetPosition = hit.point;
                if (myTransform.gameObject.GetComponent<MeshFilter>() != null) {
                    Bounds bounds = myTransform.gameObject.GetComponent<MeshFilter>().sharedMesh.bounds;
                    targetPosition.y += bounds.extents.y;
                }
                myTransform.position = targetPosition;
                Vector3 targetRotation = new Vector3 (hit.normal.x, myTransform.eulerAngles.y, hit.normal.z);
                myTransform.eulerAngles = targetRotation;
            }
        }
    }
}

Demo Script

Code snippet

using UnityEngine;
using UnityEditor;
using System.Collections;

[System.Serializable]
public class MyClass {
    public int myOtherInt;
    public float myOtherFloat;
}

[ExecuteInEditMode]
public class DemoScript : MonoBehaviour {

    [ContextMenuItem ("Set my string","SetString")]
    public string myString;

    [HideInInspector]
    public int myInt;

    [Range (2,5)]
    public float myFloat;

    public MyClass myClass;


    [SerializeField]
    private float myPrivateFloat;

    [ContextMenu ("Item added to Context Menu")]
    void TestContextMenu () {
        Debug.Log ("Context Menu");
    }

    void SetString () {
        myString = "Olive Oyl";
    }

    [MenuItem ("CONTEXT/Rigidbody/Double Mass")]
    static void DoubleMass (MenuCommand command) {
        Rigidbody rb = (Rigidbody) command.context;
        rb.mass = rb.mass * 2;
    }

    void Reset () {
        myInt = 6;
        myFloat = 7.7f;
    }

    [MenuItem ("Tools/3D/Set Mass")]
    static void SetMass () {
        Rigidbody rb = Selection.activeTransform.GetComponent<Rigidbody>();
        rb.mass = 100.0f;
    }
}

関連するチュートリアル

関連ドキュメント