Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UnityのEditorPrefsで保存した値を操作するPowerShellスクリプト
Set-Variable -Name "DefaultUnityEditorVersion" -Value 5 -Option Constant -Scope Global
function global:Get-UnityEditorPrefPath()
{
[CmdletBinding()]
[OutputType([string])]
param
(
[Parameter(Mandatory=$false)]
[int] $Version = $DefaultUnityEditorVersion
)
process
{
"HKCU:\SOFTWARE\Unity Technologies\Unity Editor {0}.x" -f $Version
}
}
function global:ConvertTo-UnityEditorPrefEntryName()
{
[CmdletBinding()]
[OutputType([string[]])]
param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string[]] $Key
)
begin
{
function GetHash([string] $value)
{
[UInt64]$max = [UInt32]::MaxValue + 1
[UInt32]$hash = 5381
foreach($char in [char[]]$value)
{
$hash = [UInt32](($hash * 33) % $max) -bxor $char
}
return $hash
}
function GetEntryName([string] $key)
{
return "${key}_h$(GetHash $key)"
}
}
process
{
return $Key | ForEach-Object { (GetEntryName $_) }
}
}
function global:Get-UnityEditorPrefEntry()
{
[CmdletBinding()]
[OutputType([string[]])]
param
(
[Parameter(Mandatory=$false, ValueFromPipeline=$true)]
[string[]] $Key,
[Parameter(Mandatory=$false)]
[int] $Version = $DefaultUnityEditorVersion
)
begin
{
$path = Get-UnityEditorPrefPath -Version $Version
$all = Get-Item -Path $path | Select-Object -ExpandProperty Property
if($null -ne $Key)
{
$nohash = $all | Foreach-Object { if($_ -match '^(.*)_h\d+$') { return $matches[1] } }
}
}
process
{
if($null -ne $Key)
{
return $nohash | Where-Object { $_ -like $Key } | ConvertTo-UnityEditorPrefEntryName
}
else
{
return $all
}
}
}
function global:Get-UnityEditorPref
{
[CmdletBinding()]
[OutputType([object[]])]
param
(
[Parameter(Mandatory=$false, ValueFromPipeline=$true)]
[string[]] $Key,
[Parameter(Mandatory=$false)]
[switch] $Json,
[Parameter(Mandatory=$false)]
[int] $Version = $DefaultUnityEditorVersion
)
begin
{
$path = Get-UnityEditorPrefPath -Version $Version
$regkey = Get-Item -Path $path
function GetEntryValue([string] $name)
{
$value = $regkey.GetValue($name)
if($value -is [Int64])
{
return [System.BitConverter]::Int64BitsToDouble($value)
}
elseif($value -is [byte[]])
{
$text = [Text.Encoding]::UTF8.GetString($value)
if(0 -eq [int]$text[-1])
{
$text = $text.SubString(0, $text.Length - 1)
}
if($Json.IsPresent)
{
try { return ConvertFrom-Json -InputObject $text } catch { return $text }
}
else
{
return $text
}
}
else
{
return $value
}
}
}
process
{
$entries = Get-UnityEditorPrefEntry -Key $Key -Version $Version
return $entries | ForEach-Object { if($_ -match '^(.*)_h\d+$') { [PSCustomObject]@{ 'Key' = $matches[1]; 'Value' = (GetEntryValue $_); 'Name' = $_} } }
}
}
function global:Remove-UnityEditorPref
{
[CmdletBinding(DefaultParameterSetName='Default')]
[OutputType([void])]
param
(
[Parameter(ParameterSetName='Default', Position=0, Mandatory=$true)]
[string[]] $Key,
[Parameter(ParameterSetName='Pipeline', Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[PSCustomObject[]] $InputObject,
[Parameter(Position=1, Mandatory=$false)]
[int] $Version = $DefaultUnityEditorVersion
)
begin
{
$path = Get-UnityEditorPrefPath -Version $Version
}
process
{
switch ($PSCmdlet.ParameterSetName) {
'Default' { $entries = Get-UnityEditorPrefEntry -Key $Key -Version $Version }
'Pipeline' { $entries = $InputObject | Select-Object -ExpandProperty Name }
}
$entries | ForEach-Object { Remove-ItemProperty -Path $path -Name $_ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.