Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active August 9, 2021 07:08
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 baobao/38bc50e2b7f4f0f840c85d3f9b1fd902 to your computer and use it in GitHub Desktop.
Save baobao/38bc50e2b7f4f0f840c85d3f9b1fd902 to your computer and use it in GitHub Desktop.
【Unity】Sprite PivotをCustomに設定するサンプルコード
using UnityEditor;
using UnityEngine;
/// <summary>
/// Sprite PivotをCustomに設定するサンプルコード
/// </summary>
public class CustomPivot : AssetPostprocessor
{
private void OnPostprocessTexture(Texture2D target)
{
var importer = assetImporter as TextureImporter;
if (importer == null) return;
importer.textureType = TextureImporterType.Sprite;
var so = new SerializedObject(importer);
// note : コメントを外すと全パラメータがログ出力されます
// PrintAllParameter(so);
var prop = so.FindProperty("m_Alignment");
// set Pivot Custom
prop.intValue = 9;
so.ApplyModifiedProperties();
}
private static void PrintAllParameter(SerializedObject so)
{
var i = so.GetIterator();
i.NextVisible(true);
string log = "";
while (i.NextVisible(false)) log += $"{i.name}\n";
Debug.Log(log);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment