2.6K Views
August 21, 22
スライド概要
可視化技術や人間計測/空間計測技術を活用した問題解決に関する研究開発。 ARコンテンツ作成勉強会(tryAR)を主催。
Getting Started with Unity
Follow Me Name︓Takashi Yoshinaga Twitter︓@Tks_Yoshinaga @Taka_Yoshinaga LinkedIn︓ https://www.linkedin.com /in /tks-yoshinaga /
Goal Of the Tutorial https://youtu.be/zD0A1vdmQM8
Launch Unity Hub Unity Hub
Create Project (1/6) ①Projects ②New Project
Create Project (2/6) Click here
Create Project (3/6) 2021.3.X
Create Project (4/6) 3D
Create Project (5/6) Project name (e.g., AR_Test) Create Project
Create Project (6/6) Unity Editor is launched.
Overview of Unity Editor Scene Object List Placing objects to design virtual environment List of folders and files in the project
Add Object With nothing selected Right-click on a blank space in the Hierarchy
Add Object ①3D Object ②Cube
Add Object Cube is addec
Add Object Let's check the view from the camera.
Play Play
Play Displayed from the camera's point of view
Stop Click Play button again to stop
注意︕︕ You can edit in Unity while it is running, but if you stop running, all edits will be discarded. Always stop execution before returning to editing!
Next Step Change the position, angle, and size of objects!
Edit Position/Angle/Size of Object① Cube
Edit Position/Angle/Size of Object① Select a mode Move Rotate Scale
Change the viewpoint of the Scene [←][→]: Move Right/Left [↑][↓]: Move Forward/Back Alt/option + Drag: Rotate
Edit Position/Angle/Size of Object② Cube
Edit Position/Angle/Size of Object② Inspector (≒Property of a object)
Edit Position/Angle/Size of Object② Set params as below Position 0 5 0 Rogation 0 0 0 Scale 1 1 1
Edit Position/Angle/Size of Object② Cube may not be visible in some view settings.
Edit Position/Angle/Size of Object② Double Click Cube
Edit Position/Angle/Size of Object② Cube will be shown at the center of scene pane
Edit Position/Angle/Size of Object② Press the [↓] key to observe from a slightly distant viewpoint
Edit Position/Angle/Size of Object② Easier to see the positional relationship between the camera and the object.
Next Step Let's change the color of an object!
⾊の設定について Cube
About Color Settings Inspector See Mesh Renderer
About Color Settings Open Materials Default-Material assigned with default color already set *Unity uses materials to set colors and textures.
About Color Settings Material details can be viewed at the bottom of Inspector, but DefaultMaterial is not editable.
Next Step Let's create editable materials!
Creating Materials Project Assets
Creating Materials Right Click
Creating Materials ①Create ②Material
Creating Materials New Material is created
Applying Material Click Cube
Applying Material MeshRenderer See Default-Material assigned to Materials
Applying Material Drag & drop over Default-Material See New Material (do not click)
Applying Material New Material Click ▷ to see details
Applying Material Click □ at the right of Albedo
Applying Material Select a color as you like (Close the window after color selection)
Next Step Let's add new details about an object e.g.: Apply physical laws
Applying Physical Law Cube Add Component
Applying Physical Law Adding new component allows you to add details that are not set by default
Applying Physical Law Physics
Applying Physical Law Rigidbody
Applying Physical Law Rigidbody is added If Use Gravity is checked, it will be affected by gravity.
Play Play
Applying Physical Law Cube will fall
Stop Click Play again to stop
Next Step Interaction between objects
Adding Object to the Scene Right Click inside Hierarchy
Adding Object to the Scene 3D Object Plane
Adding Object to the Scene Plane is added
Adding Object to the Scene Plane
Adding Object to the Scene Set Position as 0 0 0
Adding Object to the Scene Select Rotate Mode
Adding Object to the Scene Tilt slightly toward the camera.
Play Cube rolls down
Next Step Realize user interactions with C# script! e.g.: Screen click/tap
Mouse Click/Screen Tap Detection Right Click inside Hierarchy
Mouse Click/Screen Tap Detection Create Empty
Mouse Click/Screen Tap Detection Rename to ClickObject *Any name is acceptable.
Mouse Click/Screen Tap Detection Add Component
Mouse Click/Screen Tap Detection Clear text box New script
Mouse Click/Screen Tap Detection Change Script name to ClickScript *Any name is acceptable. Create and Add
Mouse Click/Screen Tap Detection ClickScript is added
Mouse Click/Screen Tap Detection Assets Double click to open ClickScript
Mouse Click/Screen Tap Detection public class ClickScript : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } }
Mouse Click/Screen Tap Detection // Start is called before the first frame update void Start() { } // Update is called once per frame Check for mouse void Update() click every frame { if (Input.GetMouseButtonDown(0)) { Debug.Log("Click!"); } }
Ctrl/Command + S To Save Script
Mouse Click/Screen Tap Detection Console
Play Playボタン
Mouse Click/Screen Tap Detection Click Screen Results are shown
Stop Click Play again to stop
Next Step Let's generate objects dynamically! e.g.: Throwing a Cube by clicking on the screen
Generate Box Dynamically Cube placed in Hierarchy already exists in space → Instead, I want to make it appear in space when the screen is clicked
Generate Box Dynamically Project
Generate Box Dynamically Cube
Generate Box Dynamically Drag & Drop Cube into Assets Folder Assets
Generate Box Dynamically A file with information about Cube with physics applied is created
Generate Box Dynamically Make a Cube appear in space from a script using data described in a file
Generate Box Dynamically // Link variables with Cube file [SerializeField] GameObject cube; void Start() { } // 毎フレーム実⾏される void Update() { if (Input.GetMouseButtonDown(0)) { Debug.Log("Click!"); } }
Generate Box Dynamically
// 毎フレーム実⾏される
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Click!");
//Generate box in the scene
GameObject obj = GameObject.Instantiate(cube);
//Acquire main camera to use its position
Camera cam = Camera.main;
//Set camera position as default position of the cube
obj.transform.position = cam.transform.position;
//Add force to shoot cube from camera
obj.GetComponent<Rigidbody>().AddForce(
10 * cam.transform.forward, ForceMode.Impulse);
}
}
Ctrl/Command + S To Save the Script
Generate Box Dynamically ClickObject
Generate Box Dynamically Drag & Drop Cube File into the right box of Cube variable Cube File
Play Play
Play Cube can be shoot form camera
Stop Click Play again to stop
Next Step Let's realize delayed processing! e.g.: Delete Cube after 10 seconds
Automatic Deletion of Cube Cube
Automatic Deletion of Cube Add Component
Automatic Deletion of Cube New script
Automatic Deletion of Cube Set script name as AutoDestroy Create and Add
Automatic Deletion of Cube AutoDestroy is added
Automatic Deletion of Cube Double Click AutoDestroy to edit
Cube Destroy Coroutine public class AutoDestroy : MonoBehaviour { void Start() { StartCoroutine(DestroyWithDelay()); } IEnumerator DestroyWithDelay() { // Erase itself after 10 seconds yield return new WaitForSeconds(10); Destroy(gameObject); } } void Update() { }
Ctrl/Command + S To Save the Script
Play Play Button
Play Cube disappears after a certain time.
Stop Click Play again to stop
Complete!