Created
November 24, 2020 05:28
-
-
Save wotakuro/513ba5d448f4f79b2c844c0d466e89c3 to your computer and use it in GitHub Desktop.
ProfilerのShader.Parseをウォッチして、ファイルに書き込む君
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Profiling; | |
using System.IO; | |
using System.Text; | |
public class ShaderParseWatcher : MonoBehaviour | |
{ | |
private Recorder shaderParse; | |
private string file; | |
private bool nextDumpFlag = false; | |
private StringBuilder sb = new StringBuilder(1024); | |
private Dictionary<int, string> allInstances; | |
private Dictionary<int, string> currenInstances; | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] | |
static void CreateShaderWatcher() | |
{ | |
var gmo = new GameObject(); | |
GameObject.DontDestroyOnLoad(gmo); | |
gmo.AddComponent<ShaderParseWatcher>(); | |
} | |
private void Awake() | |
{ | |
file = Path.Combine(Application.persistentDataPath, "shader.dump"); | |
Debug.Log("ShaderDumpText," + file); | |
this.shaderParse = Recorder.Get("Shader.Parse"); | |
this.allInstances = new Dictionary<int, string>(128); | |
this.currenInstances = new Dictionary<int, string>(128); | |
if (File.Exists(file)) | |
{ | |
File.Delete(file); | |
} | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if(nextDumpFlag) | |
{ | |
AppendDump<Shader>(file); | |
nextDumpFlag = false; | |
} | |
if(this.shaderParse.sampleBlockCount > 0 ) | |
{ | |
nextDumpFlag = true; | |
} | |
} | |
void AppendDump<T>(string path) where T:UnityEngine.Object | |
{ | |
var objs = Resources.FindObjectsOfTypeAll<T>(); | |
if(objs == null) { return; } | |
this.currenInstances.Clear(); | |
sb.Append("Frame:").Append(Time.frameCount).Append("\n"). | |
Append("instanceId,name,alive,\n"); | |
foreach( var obj in objs) | |
{ | |
int instanceId = obj.GetInstanceID(); | |
string name = obj.name; | |
this.currenInstances.Add(instanceId, name); | |
if(!this.allInstances.ContainsKey(instanceId) ){ | |
this.allInstances.Add(instanceId, name); | |
} | |
} | |
foreach(var instance in allInstances) | |
{ | |
sb.Append(instance.Key).Append(",").Append(instance.Value).Append(","); | |
sb.Append(this.currenInstances.ContainsKey(instance.Key)).Append("\n"); | |
} | |
sb.Append("\n"); | |
File.AppendAllText(path, sb.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment