LoginSignup
3
1

More than 5 years have passed since last update.

【Unity】Spriteインポート時にピボットを設定する

Posted at

はじめに

画像をプロジェクトにインポートした際に、OnPreprocessTexture()やOnPostprocessTexture()内で色々と設定を行う場合があります。今回はピボットを変更する必要があったのですが、spritePivotを設定するだけでは不十分でした。

これだけだとダメ.cs
public class ImportProcess : AssetPostprocessor
{
    void OnPreprocessTexture()
    {
        var ti = (TextureImporter)assetImporter;
        ti.spritePivot = new Vector2(0.0f, 0.0f);
    }
}

どうすればうまく行くか

spritePivotを設定する前にTextureImporterSettingsspriteAlignmentをCustomに設定してやる必要がありました。

TextureImporterSettingsを使う.cs
public class ImportProcess : AssetPostprocessor
{
    void OnPreprocessTexture()
    {
        var ti = (TextureImporter)assetImporter;

        var texSettings = new TextureImporterSettings();
        ti.ReadTextureSettings(texSettings);
        texSettings.spriteAlignment = (int)SpriteAlignment.Custom;
        ti.SetTextureSettings(texSettings);

        ti.spritePivot = new Vector2(0.5f, 0.0f);
    }
}

「Editor上でPivotをCustomに設定する作業」、に該当するコードを書く必要があったみたいです。

参考

3
1
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
3
1