Skip to content

Instantly share code, notes, and snippets.

@kankikuchi
Created June 21, 2015 22:53
Show Gist options
  • Save kankikuchi/6b035f0c65dda56a3dff to your computer and use it in GitHub Desktop.
Save kankikuchi/6b035f0c65dda56a3dff to your computer and use it in GitHub Desktop.
全ての子オブジェクトにレイヤーとマテリアル設定を行う【C#】【Unity】【拡張メソッド】
using UnityEngine;
using System.Collections;
/// <summary>
/// GameObjectの拡張クラス
/// </summary>
public static class GameObjectExtension{
/// <summary>
/// レイヤーを設定する
/// </summary>
/// <param name="needSetChildrens">子にもレイヤー設定を行うか</param>
public static void SetLayer(this GameObject gameObject, int layerNo, bool needSetChildrens = true){
if(gameObject == null){
return;
}
gameObject.layer = layerNo;
//子に設定する必要がない場合はここで終了
if(!needSetChildrens){
return;
}
//子のレイヤーにも設定する
foreach(Transform childTransform in gameObject.transform){
SetLayer (childTransform.gameObject, layerNo, needSetChildrens);
}
}
/// <summary>
/// マテリアル設定
/// </summary>
/// <param name="needSetChildrens">子にもマテリアル設定を行うか</param>
public static void SetMaterial(this GameObject gameObject, Material setMaterial, bool needSetChildrens = true){
if(gameObject == null){
return;
}
//レンダラーがあればそのマテリアルを変更
if(gameObject.GetComponent<Renderer>()){
gameObject.GetComponent<Renderer>().material = setMaterial;
}
//子に設定する必要がない場合はここで終了
if(!needSetChildrens){
return;
}
//子のマテリアルにも設定する
foreach(Transform childTransform in gameObject.transform){
SetMaterial (childTransform.gameObject, setMaterial, needSetChildrens);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment