unity_desktop_embedder 1.0.0
unity_desktop_embedder: ^1.0.0 copied to clipboard
Flutter plugin for embedding Unity textures into desktop applications using shared RAM.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:unity_desktop_embedder/unity_desktop_embedder.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late final _embedder = UnityDesktopEmbedder(
onError: (e) => setState(() => _statusMessage = "Error: $e"),
);
int? _textureId;
String _statusMessage =
"Press 'Start unity' to launch the Unity executable and start texture sharing";
bool _isLaunching = false;
//Provide the path to your Unity executable here!
final String _unityPath = Platform.isMacOS
? '/path/to/Example'
: 'C:/path/to/Example.exe';
Future<void> _startUnity() async {
setState(() {
_isLaunching = true;
_statusMessage = "Launching...";
});
try {
//Texture sizes must match the Unity project settings!
final id = await _embedder.initialize(
width: 1280,
height: 800,
executablePath: _unityPath,
);
if (!mounted) return;
setState(() {
_textureId = id != -1 ? id : null;
_isLaunching = false;
_statusMessage = id != -1 ? "Running (ID: $id)" : "Failed to register";
});
} catch (e) {
setState(() {
_isLaunching = false;
_statusMessage = "Error: $e";
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Unity Desktop Embedder')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_statusMessage),
const SizedBox(height: 20),
Expanded(
child: Center(
child: switch ((_textureId, _isLaunching)) {
(int id, _) => UnityDesktopTexture(textureId: id),
(_, true) => const CircularProgressIndicator(),
_ => TextButton(
onPressed: _startUnity,
child: const Text('Start Unity'),
),
},
),
),
],
),
),
),
);
}
@override
void dispose() {
_embedder.dispose();
super.dispose();
}
}