0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Unity オブジェクトをボタンから拡大縮小するときはOnClickでthis.transform.localScale = new Vector3(1, 1, 1);をしよう!

Posted at

やること

2分でオブジェクトを拡大するボタンを作る

完成イメージ

ボタンを押す前
スクリーンショット 2020-07-05 11.47.07.png
ボタンを押した後
スクリーンショット 2020-07-05 11.47.15.png

使用したバージョン

Unity 2019 3.14

さっそくやってみる!

スクリプトをつくろう!

名前はなんでもいいです。
私はScaleChengeというスクリプト名にしました。

コードをかこう!

ScaleChange.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScaleChenge : MonoBehaviour
{
    void Start()
    {
        
    }

    public void OnClick()
    {
        this.transform.localScale = new Vector3(1, 1, 1);
    }
}

スクリプトを拡大したいオブジェクトにアタッチしよう!

私の場合は、carparentにアタッチしました。
スクリーンショット 2020-07-05 11.50.48.png

ボタンをつくろう!

(ボタンの作り方は割愛します)
つくったボタンのインスペクター > Buttonに下の画像のようなOnClickという場所があるのでそこの左下のところに先ほどスクリプトをアタッチしたオブジェクトをつけましょう!

スクリーンショット 2020-07-05 11.52.01.png

すると、右側のところから選択できるので
ScaleChange(スクリプト名) > OnClickを選択しましょう!

これであとは再生ボタンをおして、ボタンをクリックすれば完成です!

縮小したいとき

もう1つボタンをつくってあげて
スクリプト内の
Vector3(0.3,0.3,0.3)とかにしてあげれば縮小できると思います!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?