UIのPrefabを追加するときのWarning

Prefab化したUIパーツをCanvasに、スクリプトでアタッチしようとしたらWarningが出た。

Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues.
UnityEngine.Transform:set_parent(Transform)

GameObject o = Instantiate( Foo );
o.transform.parent = this.transform;

こういう様に親GameObjectを指定するのは良くはなく、以下が正しい。

GameObject o = Instantiate( Foo );
o.transform.SetParent(this.transform);

Via! http://haraken.hatenablog.com/entry/2016/11/11/RectTransform%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%82%BF%E3%83%B3%E3%82%B9%E3%81%A8%E3%82%B5%E3%82%A4%E3%82%BA%E3%81%AE%E8%A9%B1

親オペレータの名前からID番号を取得する

オペレータをまとめたBase COMPを複製し、それぞれのオペレータで挙動を変える場合に、各COMPの名前からID番号を取得し、それを元に挙動を変える方法。

例えば、次の様な同じ内容の持つBase COMPを持つプロジェクトを作る。

複製するCOMPは、”item1″,”item2″と命名し、接尾に連番をつけている。(itemN)

item1を開くと、次の様なネットワークになっている。

概要としては、2つのConstant CHOPの値をSwitch CHOPで切り替えて、out CHOPで出力している。Switch CHOPのプロパティで、親オペレータの名前の接尾の数字をIDとして、取得し、偶数か奇数かを判別、偶数だったら1(constant1の値)、奇数だったら-1(constant2の値)を選んでいる。


switch1のプロパティを見ると、indexの項目に、expressionが入っている(水色になるモード)。

me.parent().digits%2

これは、pythonのコード。me.parent()で親ノードの参照を取得できる。取得した名前から数字を参照するのがdigits。それの2で割ったときの余割(割ったときの余り)を出して、偶数か奇数かを判別している。

item2も同じ内容になっており、こちらは偶数なので1を出力されるようになっている。

これを応用すると、ID毎に座標を変えたり、大きさを変えたりなどの挙動を変えることができる。

サンプルtoe
https://github.com/arkwknsk/touchdesigner/blob/master/tips/digits/digits.toe

High Sierraでgitが使えなくなったら

MacをHigh Sierraにアップグレードしたところ、gitが使えなくなってしまった

$ git xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

Developerツールを再インストールさせなければならないらしい。

unity$ xcode-select --install
xcode-select: note: install requested for command line developer tools

確認ダイアログが出るのでOKを押せば、インストールが始まる。インストールを完了すると、これまで通り、gitが使える様になる。

Via! https://e-yota.com/idle_talk/mac-os-high-sierra_git_xcrun_error/

ofMapみたいなMapping&Scaling

ofMapのように、任意の数値を定められた数値範囲変換(スケーリング、数学的には線形写像というらしい)をC#でできるようにする。
独自のユーティリティクラスを作る方法もあるが、既存のfloat型を拡張する形での実装方法。

以下のコードを、Map.csなどとして、スクリプトを置くフォルダ(ex. /Scripts)に保存する

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class ExtensionMethods
{
    public static float Map (this float value, float fromSource, float toSource, float fromTarget, float toTarget)
    {
        return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
    }

}

使用方法は以下の通り

float foo = 0.5f;
float result = foo.Map( 0.0f, 1.0f, 0, 100 );
//50

Via! https://stackoverflow.com/questions/14353485/how-do-i-map-numbers-in-c-sharp-like-with-map-in-arduino