unity2moveit2/scripts/setup-unity-environment.ps1
ayuan9957 fe15edcbd5 Initial commit: Unity-MoveIt2 integrated robotic arm simulation system
- Unity frontend with ROS-TCP-Connector for ROS2 communication
- Docker-based ROS2 Jazzy backend with MoveIt2 integration
- Support for 1-9 DOF manipulators
- UR5 robot configuration and URDF files
- Assembly task feasibility analysis tools
- Comprehensive documentation and deployment guides

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-13 12:08:34 +08:00

221 lines
8.7 KiB
PowerShell

# Unity Environment Setup Script for ROS2 Integration
# 配置Unity环境以支持ROS2机器人控制系统
param(
[switch]$CheckVersion,
[switch]$OpenProject,
[switch]$UpdatePackages,
[switch]$VerifySetup,
[string]$UnityPath = "C:\Program Files\Unity\Hub\Editor\2021.3.33f1c1\Editor\Unity.exe"
)
# 项目路径配置
$ProjectRoot = Split-Path -Parent $PSScriptRoot
$PickAndPlaceProject = Join-Path (Split-Path -Parent $ProjectRoot) "PickAndPlaceProject"
$UnityProject = Join-Path $ProjectRoot "unity-project"
Write-Host "=== Unity Environment Setup Script ===" -ForegroundColor Green
Write-Host "Unity Path: $UnityPath" -ForegroundColor Cyan
Write-Host "Project Root: $ProjectRoot" -ForegroundColor Cyan
Write-Host "PickAndPlace Project: $PickAndPlaceProject" -ForegroundColor Cyan
function Test-UnityInstallation {
Write-Host "`nChecking Unity installation..." -ForegroundColor Yellow
if (Test-Path $UnityPath) {
Write-Host "Unity 2021.3.33f1c1 found at: $UnityPath" -ForegroundColor Green
# 获取Unity版本信息
try {
$versionInfo = & $UnityPath -version 2>&1 | Out-String
Write-Host "Unity Version Info: $versionInfo" -ForegroundColor Gray
} catch {
Write-Host "Could not retrieve version info, but Unity executable exists" -ForegroundColor Yellow
}
return $true
} else {
Write-Host "Unity not found at specified path!" -ForegroundColor Red
Write-Host "Please verify Unity installation path" -ForegroundColor Red
return $false
}
}
function Test-ProjectCompatibility {
Write-Host "`nChecking project compatibility..." -ForegroundColor Yellow
$projectVersionFile = Join-Path $PickAndPlaceProject "ProjectSettings\ProjectVersion.txt"
if (Test-Path $projectVersionFile) {
$content = Get-Content $projectVersionFile
$editorVersion = ($content | Where-Object { $_ -match "m_EditorVersion:" }) -replace "m_EditorVersion: ", ""
Write-Host "Current Project Version: $editorVersion" -ForegroundColor Gray
Write-Host "Target Unity Version: 2021.3.33f1c1" -ForegroundColor Gray
if ($editorVersion -like "2022.*") {
Write-Host "Project was created with Unity 2022.x" -ForegroundColor Yellow
Write-Host "Downgrading to Unity 2021.3 may require package updates" -ForegroundColor Yellow
return $false
} elseif ($editorVersion -like "2021.3.*") {
Write-Host "Project version compatible with Unity 2021.3" -ForegroundColor Green
return $true
} else {
Write-Host "Unknown version compatibility" -ForegroundColor Yellow
return $false
}
} else {
Write-Host "Project version file not found!" -ForegroundColor Red
return $false
}
}
function Test-ROSPackages {
Write-Host "`nChecking ROS packages..." -ForegroundColor Yellow
$manifestFile = Join-Path $PickAndPlaceProject "Packages\manifest.json"
if (Test-Path $manifestFile) {
$manifest = Get-Content $manifestFile -Raw | ConvertFrom-Json
# 检查关键的ROS包
$requiredPackages = @{
"com.unity.robotics.ros-tcp-connector" = "ROS TCP Connector";
"com.unity.robotics.urdf-importer" = "URDF Importer"
}
$allPackagesFound = $true
foreach ($package in $requiredPackages.Keys) {
if ($manifest.dependencies.PSObject.Properties.Name -contains $package) {
$version = $manifest.dependencies.$package
Write-Host "$($requiredPackages[$package]): $version" -ForegroundColor Green
} else {
Write-Host "Missing: $($requiredPackages[$package])" -ForegroundColor Red
$allPackagesFound = $false
}
}
return $allPackagesFound
} else {
Write-Host "Package manifest not found!" -ForegroundColor Red
return $false
}
}
function Update-ProjectVersion {
Write-Host "`nUpdating project version..." -ForegroundColor Yellow
$projectVersionFile = Join-Path $PickAndPlaceProject "ProjectSettings\ProjectVersion.txt"
if (Test-Path $projectVersionFile) {
$newContent = @(
"m_EditorVersion: 2021.3.33f1c1"
"m_EditorVersionWithRevision: 2021.3.33f1c1 (b9a993fd2ece)"
)
Set-Content -Path $projectVersionFile -Value $newContent -Encoding UTF8
Write-Host "Project version updated to Unity 2021.3.33f1c1" -ForegroundColor Green
} else {
Write-Host "Could not update project version file" -ForegroundColor Red
}
}
function Open-UnityProject {
Write-Host "`nOpening Unity project..." -ForegroundColor Yellow
if (Test-Path $PickAndPlaceProject) {
Write-Host "Opening project: $PickAndPlaceProject" -ForegroundColor Cyan
# 启动Unity并打开项目
Start-Process -FilePath $UnityPath -ArgumentList "-projectPath `"$PickAndPlaceProject`"" -NoNewWindow
Write-Host "Unity project opening..." -ForegroundColor Green
Write-Host "Please wait for Unity to load and resolve packages" -ForegroundColor Gray
} else {
Write-Host "Project directory not found!" -ForegroundColor Red
}
}
function Show-SetupInstructions {
Write-Host "`n=== Unity Setup Instructions ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "1. Project Compatibility Check:" -ForegroundColor Yellow
Write-Host " - Current project created with Unity 2022.x"
Write-Host " - Needs downgrade to Unity 2021.3.33f1c1"
Write-Host " - May require package re-import"
Write-Host ""
Write-Host "2. ROS Package Verification:" -ForegroundColor Yellow
Write-Host " - ROS TCP Connector: Installed (v0.7.0)"
Write-Host " - URDF Importer: Installed (v0.5.2)"
Write-Host ""
Write-Host "3. Recommended Steps:" -ForegroundColor Yellow
Write-Host " a) Backup current project"
Write-Host " b) Open project with Unity 2021.3.33f1c1"
Write-Host " c) Allow Unity auto upgrade/downgrade"
Write-Host " d) Check ROS packages in Package Manager"
Write-Host " e) Re-import necessary assets" -ForegroundColor Yellow
Write-Host ""
Write-Host "4. Connection Configuration:" -ForegroundColor Yellow
Write-Host " - ROS TCP Endpoint: localhost:10000"
Write-Host " - Test connection after Docker container is running"
Write-Host ""
}
function Test-NetworkConnectivity {
Write-Host "`nTesting network connectivity..." -ForegroundColor Yellow
# 测试Docker容器端口
$rosPort = 10000
try {
$connection = Test-NetConnection -ComputerName "localhost" -Port $rosPort -WarningAction SilentlyContinue
if ($connection.TcpTestSucceeded) {
Write-Host "ROS TCP Endpoint accessible on port $rosPort" -ForegroundColor Green
} else {
Write-Host "ROS TCP Endpoint not accessible (Docker container may not be running)" -ForegroundColor Yellow
}
} catch {
Write-Host "Could not test network connectivity" -ForegroundColor Yellow
}
}
# 主执行逻辑
if ($CheckVersion) {
Test-UnityInstallation
Test-ProjectCompatibility
Test-ROSPackages
} elseif ($OpenProject) {
if (Test-UnityInstallation) {
Update-ProjectVersion
Open-UnityProject
}
} elseif ($UpdatePackages) {
Write-Host "Package update functionality will be implemented after Unity opens" -ForegroundColor Yellow
Show-SetupInstructions
} elseif ($VerifySetup) {
Test-UnityInstallation
Test-ProjectCompatibility
Test-ROSPackages
Test-NetworkConnectivity
Show-SetupInstructions
} else {
# 默认执行完整检查
Write-Host "Running complete Unity environment check..." -ForegroundColor Cyan
$unityOK = Test-UnityInstallation
$projectOK = Test-ProjectCompatibility
$packagesOK = Test-ROSPackages
if ($unityOK -and $projectOK -and $packagesOK) {
Write-Host "`nUnity environment ready!" -ForegroundColor Green
Write-Host "Use -OpenProject to launch Unity with the project" -ForegroundColor Cyan
} else {
Write-Host "`nUnity environment needs attention" -ForegroundColor Yellow
Show-SetupInstructions
}
}
Write-Host "`n=== Available Commands ===" -ForegroundColor Cyan
Write-Host ".\setup-unity-environment.ps1 -CheckVersion # Check Unity and project versions"
Write-Host ".\setup-unity-environment.ps1 -OpenProject # Open Unity project"
Write-Host ".\setup-unity-environment.ps1 -VerifySetup # Complete environment verification"
Write-Host ".\setup-unity-environment.ps1 # Run default checks"