Programming」カテゴリーアーカイブ

プログラミング全般に関するカテゴリーです。

UnityのLighting設定をスクリプトから設定する方法

Lighting設定をスクリプトから設定したい場合はRenderSettingsを使うと良いそうです。
例えば、Ambient Intensityを設定する場合は次のような感じです。

RenderSettings.ambientIntensity = 2.0f;

リンク

Unity – Scripting API: RenderSettings
https://docs.unity3d.com/ScriptReference/RenderSettings.html

Access Lighting window properties in Script ? – Unity Forum
https://forum.unity.com/threads/access-lighting-window-properties-in-script.328342/

UnityのCustomEditorでInspector表示を強制的に再描画する方法

UnityでCustomEditorを使っていて、内容が更新されているのにInspectorの表示が更新されないときはEditorUtility.SetDirty(target)を実行してみると良いそうです。

void OnInspectorGUI()
{
  EditorUtility.SetDirty(target);
}

リンク

How do you force a custom inspector to redraw? – Unity Answers
https://answers.unity.com/questions/333181/how-do-you-force-a-custom-inspector-to-redraw.html

JavaScriptの分割代入を使って配列をシャッフル

JavaScriptの配列をシャッフルしようと思ってお手軽な方法を調べてみました。

Destructuring assignment

Destructuring assignmentの使い方は次のような感じです。

a = [ 1, 2, 3, 4, 5 ];
[a[1], a[2]] = [a[2], a[1]];

結果はa = [ 1, 3, 2, 4, 5 ]となります。

シャッフルの実装

ということで、これを複数回繰り返すと配列のシャッフルができるという仕組みです。

for(i = 0; i < 5; ++i) {
  j = Math.floor(Math.random()*a.length);
  [a[i], a[j]] = [a[j], a[i]];
}

もう少し効率的な方法もあるかなと思いますが、Destructuring assignment自体は便利そうなので使っていこうかなと思います。

リンク

Destructuring assignment – JavaScript | MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment