Skip to content

Instantly share code, notes, and snippets.

@Buravo46
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Buravo46/11109667 to your computer and use it in GitHub Desktop.
Save Buravo46/11109667 to your computer and use it in GitHub Desktop.
【Unity】基底クラスを継承した派生クラスが、基底クラスの関数を処理する一例。処理したい基底クラスの関数を仮想関数化し、派生クラスで処理したい関数をオーバーライドする。
using UnityEngine;
using System.Collections;
/// <summary>
/// 惑星クラス
/// </summary>
/// <remarks>
/// 基底クラス
/// </remarks>
public class Planet : MonoBehaviour {
/// <summary>
/// 回転するかどうかを判定するフラグ
/// </summary>
public bool isRotate = true;
/// <summary>
/// 回転速度
/// </summary>
public Vector3 rotateSpeed = new Vector3(0.0f, 10.0f, 0.0f);
/// <summary>
/// 毎フレームごとの処理
/// </summary>
/// <remarks>
/// 仮想関数化
/// </remarks>
public virtual void Update () {
}
/// <summary>
/// 回転する処理
/// </summary>
/// <remarks>
/// 仮想関数化
/// </remarks>
public virtual void IsRotate(){
if(isRotate){
gameObject.transform.Rotate(rotateSpeed.x * Time.deltaTime, rotateSpeed.y * Time.deltaTime, rotateSpeed.z * Time.deltaTime);
}
}
}
using UnityEngine;
using System.Collections;
/// <summary>
/// 太陽クラス
/// </summary>
/// <remarks>
/// 基底クラスを継承した派生クラス
/// </remarks>
public class Sun : Planet {
/// <summary>
/// 待ち時間
/// </summary>
private float waitingTime = 0;
/// <summary>
/// 毎フレームごとの処理
/// </summary>
/// <remarks>
/// 仮想関数をオーバーライドした関数
/// </remarks>
public override void Update () {
// 仮想化した関数を基底クラスから呼び出す
base.IsRotate();
waitingTime += Time.deltaTime;
if(waitingTime >= Time.deltaTime * 300){
waitingTime = 0;
Debug.Log("InitWaitingTime");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment